This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to push JSON Object to array in AngularJs  RSS feed

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

$scope.subject.question.push($scope.questionList[i].id);
error:
Cannot read property 'push' of undefined

Can anyone correct this?
 
Master Rancher
Posts: 3887
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$scope.subject.question

There is no question attribute in subject.
At least not in the code you've posted.
 
priyanshi bhardwaj
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:$scope.subject.question

There is no question attribute in subject.
At least not in the code you've posted.


Now the attribute is questions but still the error remains same.
 
Dave Tolls
Master Rancher
Posts: 3887
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure?
Because $scope.subject.questions is not "undefined" so I would expect a different error.
 
priyanshi bhardwaj
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed little bit in the structure of questions



$scope.subject .questions.push($scope.subject .questions.description=$scope.questionList[i].id,$scope.subject .questions.questionId=$scope.questionId);
Is this the correct way of doing it ?




 
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your $scope.subject.questions array contains objects. That means you need to add them as objects:
 
priyanshi bhardwaj
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Your $scope.subject.questions array contains objects. That means you need to add them as objects:


Thanks it worked out for me ..
I am having an another question if I want to save this subject into my SQL database using java so what should be my approach ?
 
Rob Spoor
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post it using an AJAX call to a back-end service, that will do the actual inserting. You can use a JSON or form-style request body.
 
priyanshi bhardwaj
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And  one more question if I call add Question function multiple times in a single session so the data which I have inserted in the last call gets overwrite or it just adds further.
 
priyanshi bhardwaj
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Post it using an AJAX call to a back-end service, that will do the actual inserting. You can use a JSON or form-style request body.


I have thought of posting  subject through http request post to my controller and then saving it in my DB in arraylist format while mapping questions field with another table. Is this a correct way of doing it?
 
Sheriff
Posts: 24086
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote:saving it in my DB in arraylist format



I'm not sure what you mean by that, but it doesn't sound like a good idea to me. I would suggest designing a properly normalized database for a start, and have the back end parse the data it receives and store that data in suitable tables.
 
priyanshi bhardwaj
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

priyanshi bhardwaj wrote:saving it in my DB in arraylist format



I'm not sure what you mean by that,


Array list I mean to say list of questions I'd would be stored in the table known as subjects and those questions id will be mapped in another table with description id .
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!