node.js - SailsJS One to Many associations -
hi i'm building sails app couple of models in 1 many configuration. i've been able create new instances of models via relationship, seems working 1 way.
for reference (one) project can have (many) users.
/** projects.js **/ name: { type: string }, managers: { collection: 'users', via: 'projects' } /** user.js **/ username: { type: 'string' }, projects { { model: 'projects' }
using following create method, results in user have reference project, no reference user available project. managers reference array of user ids.
project.create({ name: req.param('name'), managers: req.param('managers') }).exec(function newproject(err, results) { if (err) return res.json({ error: err }); return res.json({ results: results }); });
and retrieve projects respective managers:
project.findone({ id: req.param('id') }).populate('managers').exec(function project(err, results) { if (err) return res.json({ error: err }); return res.json({ results: results }); });
shouldn't
managers: { collection: 'users', via: 'projects' }
be this:
managers: { collection: 'user', via: 'projects' }
?...
you refer collection model filename. refer documentation
Comments
Post a Comment