dimanche 29 mars 2015

Sails.JS findOne not returning Association

I have a function to find a resource based on it's geo coordinates, but the findOne method doesn't return the comments collection from the model. If I use the blueprint route to find based on ID, I get the comments along with it.


find:id JSON



{
"comments": [
{
"text": "Some Text",
"nomination": "551865064a5ccf41274be682",
"createdAt": "2015-03-29T21:02:10.586Z",
"updatedAt": "2015-03-29T21:02:10.586Z",
"id": "55186852d21850a627292db3"
}
],
"name": "Karma Bird House",
"address": "47 Maple Street Burlington, VT 05401",
"geo": "44.473231,-73.217882",
"lat": 44.473231,
"lon": -73.217882,
"streetImg": "http://ift.tt/1ONRa1L",
"votes": 1,
"createdAt": "2015-03-29T20:48:06.109Z",
"updatedAt": "2015-03-29T21:02:48.817Z",
"id": "551865064a5ccf41274be682"
}


findGeo:id JSON



{
"nom": {
"name": "Karma Bird House",
"address": "47 Maple Street Burlington, VT 05401",
"geo": "44.473231,-73.217882",
"lat": 44.473231,
"lon": -73.217882,
"streetImg": "http://ift.tt/1ONRa1L",
"votes": 1,
"createdAt": "2015-03-29T20:48:06.109Z",
"updatedAt": "2015-03-29T21:02:48.817Z",
"id": "551865064a5ccf41274be682"
}
}


findGeo Method



findGeo: function (req, res, next) {
Nomination.findOne({geo: req.param('id')}, function foundNomination(err, nom) {
if(err) return next(err);
if(!nom) return next();

return res.json({
nom: nom
});
})
}


Model:



module.exports = {

attributes: {
id:{
type:"int",
primaryKey: true,
autoIncrement: true
},
// Location's name
name: {
type: 'string',
required: true
},

// Locations Lat and lon
lat: 'float',

lon: 'float',

// lat,lon
geo: {
type: 'string',
unique: true
},

// Location's Address
address: {
type: 'string',
required: true
},

// Number of Votes
votes: {
type: 'integer',
defaultsTo: '0'
},

// Street View Image
streetImg: 'string',

// Association of comments
comments: {
collection: 'comment',
via: 'nomination'
}

}
};

Aucun commentaire:

Enregistrer un commentaire