EmbedsMany: Unable to transfer properties

38 views
Skip to first unread message

Spencer Strombotne

unread,
Feb 25, 2015, 6:55:07 PM2/25/15
to
I'm creating an EmbedsMany relationship with the following code based on http://docs.strongloop.com/display/public/LB/Embedded+models+and+relations#Embeddedmodelsandrelations-Advancedexample:embedwithbelongsTo :

    var Location = app.models.location;
   
var EmployeeLocation = app.models.employeeLocation;
   
var Employee = app.models.employee;

   
EmployeeLocation.belongsTo(Location, {
        foreignKey
: 'id', // re-use the actual location id
        properties
: {
            id
: 'id',
            address
: 'address',
            state
: 'state',
            city
: 'city',
            zip
: 'zip'
       
}, // denormalize, transfer id
       
// For info on invertProperties see https://github.com/strongloop/loopback-datasource-juggler/pull/219
        options
: {
            invertProperties
: true
       
}
   
});

   
Employee.embedsMany(EmployeeLocation, {
       
as: 'locations',
        scope
: {
            include
: 'location'
       
}
   
});

This works and embeds locations in the employee model but get the following response from the api:

 /api/employees?filter=%7B%22include%22%3A%5B%22locations%22%5D%7D
{
   
"id": "54ee4c09d6f895de8d3ee978",
   
"employeeLocations": [{
       
"id": "54ee3efb9ed34efb826d0a72"
   
}, {
       
"id": "54ee4c3cd6f895de8d3ee97d"
   
}],
   
"locations": [{
       
"id": "54ee3efb9ed34efb826d0a72",
       
"location": {
            "id": "54ee3efb9ed34efb826d0a72",
            "address": "12345 Front St",
           
"state": "MA",
           
"city": "Freetown",
           
"zip": "27513"
       
}
   
}, {
       
"id": "54ee4c3cd6f895de8d3ee97d",
       
"location": {
            "id": "54ee4c3cd6f895de8d3ee97d", 
            "address": "123 Main Str",
           
"state": "TX",
           
"city": "Dallas",
           
"zip": "75857"
       
}
   
}]
}



This is great but I'd rather have:
{
    "id": "54ee4c09d6f895de8d3ee978",
    "employeeLocations": [{
        "id": "54ee3efb9ed34efb826d0a72"
    }, {
        "id": "54ee4c3cd6f895de8d3ee97d"
    }],
    "locations": [{
        "id": "54ee3efb9ed34efb826d0a72",
        "address": "12345 Front St",
        "state": "MA",
        "city": "Freetown",
        "zip": "27513"

    }, {
        "id": "54ee4c3cd6f895de8d3ee97d",
        "address": "123 Main Str",
        "state": "TX",
        "city": "Dallas",
        "zip": "75857"
    }]
}

Loopback seems to be ignoring the property and invertProperties options that I've used since the json response doesn't change if I exclude them from my model definition.

What am I missing here?

Reply all
Reply to author
Forward
0 new messages