GregMc
Posts: 0
Joined: Wed Dec 03, 2014 12:46 am

Posting array to database only creates one row

In my app I build a array of answers to a quiz. At the end of the quiz when the user submits the answers I would like to post the answers to a database collection, each answer which is one item in the array should occupy one row in the collection.

The array of answers is in session storage. I have created a created service to allow creating new rows in the collection. In the before send mapping I have mapped the items from the array in local storage to the request body.

The problem I am having is that only the first item in the array is being posted to a row in the collection. I have the iterator mapped so I am not sure what the problem is. Here is a screenshot of the mappings:

Image

I assume there is no problem with the data types since one row is being created successfully but I do not understand why the iterator is not working?

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Posting array to database only creates one row

Hi Greg,

We need more details about your problem:
ol

liPlease attach screenshots of your iterator./li
liPlease check the content of your array. Maybe you are saving only the one item in local storage variable?/li
liAll the items of array have same structure like a first one? If the second item in array hasn't one or has more fields that you have mapped then service will not work properly./li
/ol

GregMc
Posts: 0
Joined: Wed Dec 03, 2014 12:46 am

Posting array to database only creates one row

Hi Ilya,

Not sure what you mean by a screen shot of the iterator, I have provide a screen shot of the iterator mapping in the original post. Here is some additional info. The local storage variable that the answers are captured to is an array type. It is an array of objects that I have defined in the model. A screenshot of the model definitions for the answer object and array of answers is below:

Image

Here you can see the definition of the answer object and the quiz_answers array. The next screen shot shows the local storage variables, see the last one named quiz_answers is of type quiz_answer.
Image

Next screen shot please observe the value for the variable in debugger named ans_from_stor, It contains 4 answer objects with associated fields. This is the variable that will get assigned to the local storage variable before the posting to the database.

Image

Last step before the post is to observe the value of the local storage variable before the create service is invoked. See in the sceenshot below the value of the quiz answers variable, the array variable contains several question objects.

Image

The last screenshot shows the POST service response, see that only the first item from the array was included in the payload.

Image

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Posting array to database only creates one row

Hi GregMc,

Unfortunatly currently only one item could be linked via mapping.

To add several items you have two options:

1.1. Create JS array manually(from state of your components).
1.1. Invoke create service in a loop(for each item you want to add to DB).

2.1. Create JS array manually(from state of your components).
2.2. Invoke server script with this array. Inside server script you can add all passed items to DB collection

Regards.

GregMc
Posts: 0
Joined: Wed Dec 03, 2014 12:46 am

Posting array to database only creates one row

Thanks for the clarification Yuri, I have done option 1 and it seems to be working well.

Thanks.

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Posting array to database only creates one row

Hi
Is there no other way to do this. Option 1 is the only way I can go as I dont use the Appery database.

However, wont Option 1 be too slow if you have thousands of items in your array?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Posting array to database only creates one row

Yes, you are right, it will take a lot of time

Return to “Issues”