creating model with array of objects

3,871 views
Skip to first unread message

vj

unread,
Dec 1, 2014, 10:56:36 PM12/1/14
to loopb...@googlegroups.com
Hi 

I am new to node and loopback.
by referring to loopback documentation, i am trying to create a new model with mongodb as the datastore. 

In mongo db i want to store a de-normalized customer-address information where one customer can have multiple addresses, something like

{
   _id:{type: Number},
   FirstName:{type:String},
   LastName: {type:String},
   Address:[{
AddressType:{type:String},
         AddressLine:{type:String}
}]
}

How will I create a model for this? 
started with slc loobkac:model but got stuck on how to define the object, when property type is "object".
{
  "name": "Customer",
  "plural": "Customers",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "_id": {
      "type": "Number",
      "required": true
    },
     
    "Address": {
      "type": "object"
        
    } 
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}



Thanks
Vijay

Simon Ho

unread,
Dec 2, 2014, 2:26:26 AM12/2/14
to loopb...@googlegroups.com
```
...
"Address": {
  "type": [
    "object"
  ],
  "required": true
}
...
```

Raymond Feng

unread,
Dec 2, 2014, 2:43:41 AM12/2/14
to vj, loopb...@googlegroups.com
‘slc loopback:model’ command doesn’t support the definition of nesting properties. But you can modify the json file manually. See an example of such model at:


Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Felipe Figueroa

unread,
Dec 4, 2014, 6:59:48 AM12/4/14
to loopb...@googlegroups.com, aro...@uw.edu
This is only needed if you want to enforce a given structure on subdocuments, but any property of type Object would allow nested attributes without complaints.

Moreover, you can insert root level properties you haven't declared on the json file. It seems you won't be able to filter on these properties afterwards, but that's just a minor drawback.

Charles Naccio

unread,
Feb 26, 2015, 7:57:35 PM2/26/15
to loopb...@googlegroups.com
I'm having a similar problem... When using the definition below lines is being stored as [Object object] in database....


"lines": {
         
"type": [
             
{
                 
"type": "string",
                 
"name": "string",
                 
"description": "string",
                 
"unitCost": "number",
                 
"quantity": "number",
                 
"amount": "number"
             
}
         
],
         
"required": true
     
}

建云谢

unread,
Mar 14, 2016, 11:16:35 PM3/14/16
to LoopbackJS, aro...@uw.edu, ray...@strongloop.com
but this is not the json definition file, can I found any json file example on the array of concrete object?

Shashank Komuroju

unread,
Dec 29, 2016, 6:04:12 AM12/29/16
to loopb...@googlegroups.com
@Charles :: (I know it's too late...)

{"lines": {
          "type": [
              
{
                  "
type":{
                      "type": "string",
                      "name": "string",
                      
"description": "string",
                      
"unitCost": "number",
                      
"quantity": "number",
                      "amount": "number" 
                  }
              
}
          
],
          
"required": true
      
}
}
Even this model has problem.  When we fetch the data with any get call, it renders this particular field as ["Object Object"] even though the data is properly saved in the database.

James Ong

unread,
Apr 12, 2018, 1:12:53 PM4/12/18
to LoopbackJS
There is an issue in the JSON object definition with the use of the reserved keyword "type" as a property of the object - it confuses the Loopback parser and so it just sets the element to "Object Object".  The following should work (note the change of "type" to "lineType" in the JSON object definition):


{"lines": {
          "type": [
              
{
                  "
type":{
                      "lineType": "string",
                      "name": "string",
                      
"description": "string",
                      
"unitCost": "number",
                      
"quantity": "number",
                      "amount": "number" 
                  }
              
}
          
],
          
"required": true
      
}


Regards,
James
Reply all
Reply to author
Forward
0 new messages