Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array of objects are saved as [Object Object] #65

Closed
zmarouf opened this issue Jan 2, 2015 · 4 comments
Closed

Array of objects are saved as [Object Object] #65

zmarouf opened this issue Jan 2, 2015 · 4 comments
Assignees
Labels
Milestone

Comments

@zmarouf
Copy link

zmarouf commented Jan 2, 2015

Hello!

I've been playing with loopback for a few days and I'm having trouble with a specific field in my model:

 "myField": {
   "type": [
     "object"
   ],
   "required": false
 }

If I try to set it like so:

...
myModel.myField = [{'atr1': "value1", 'atr2': "value2"}];
...
MyModel.upsert(myModel, function (err) {
  if (err) console.log(err);
});

I get something weird in the resulting row field:

+-----------------+                         +----------------------------------------+
| myField         |                         | myField                                |
+-----------------+        instead of       +----------------------------------------+
| [object Object] |                         | [{"atr1":"value1","atr2":"value2"}]    |
+-----------------+                         +----------------------------------------+

Could someone enlighten me as to what I'm doing wrong?

@zmarouf
Copy link
Author

zmarouf commented Jan 2, 2015

Further tests show that it happens with the way I use upsert or updateAttributes. I changed my code to use the static updateAll and it works now.

I still don't understand why what I was doing is wrong though, so if anyone has an explanation for me, I'm all ears :)

@smehrbrodt
Copy link

I think this is fixed with 634a0a9

@johannesjo
Copy link

This happens to me also with the standard create method.

@b-admike
Copy link
Contributor

b-admike commented Mar 31, 2017

@zmarouf I have tried your scenario with upsertWithWhere and was able to successfully get the following:
Steps to reproduce:

  1. create a LB 2.x app with empty-server template
  2. create a mysql datasource using the generator prompt slc loopback:datasource
  3. use slc loopback:model to create myModel
  4. make create-mymodel.js which calls automigrate function on myModel
  5. start app and in explorer do a POST request to myModels with following value:
{   "myField": [     {"atr1": "value1",      "atr2": "value2"     }   ] }
  1. Check database
mysql> select * from myModel;
+----+-------------------------------------+
| id | myField                             |
+----+-------------------------------------+
|  1 | [{"atr1":"value1","atr2":"value2"}] |
+----+-------------------------------------+
1 row in set (0.00 sec)

  1. do a POST request to UpsertWithWhere endpoint and specify the following:
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
  \"myField\": [
    {\"atr1\": \"value3\",
     \"atr2\": \"value4\"
    }
  ]
}" "http://localhost:3000/api/myModels/upsertWithWhere?where=%7B%20%20%20%22myField%22%3A%20%5B%20%20%20%20%20%7B%22atr1%22%3A%20%22value1%22%2C%20%20%20%20%20%20%22atr2%22%3A%20%22value2%22%20%20%20%20%20%7D%20%20%20%5D%20%7D"
  1. check database again
mysql> select * from myModel;
+----+-------------------------------------+
| id | myField                             |
+----+-------------------------------------+
|  1 | [{"atr1":"value3","atr2":"value4"}] |
+----+-------------------------------------+
1 row in set (0.01 sec)

I have created a sample repo which has the app that can verify the issue is fixed here

@b-admike b-admike added this to the Sprint 32 milestone Mar 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants