How do I use collections in MongoDB and sails.js? -
problem:
i want attribute 1 model nested in another.
my solution:
i have 2 models in application, 1 users , other events:
user:
attributes: { name: 'string', email: 'string', events: { collection: 'event', via: 'author' } }
event:
attributes: { date: 'string', time: 'string',, other: 'string', author: { model: 'user' } }
one user should able create multiple events, 1 event may have 1 author. don't know how should make queries creating events , showing them.
for creating event query:
var newevent = { date: req.param("date"), time: req.param("time"), other: req.param("other"), author: req.user } event.create(newevent).exec(function(err, model){ console.log("new event created"); console.log(model); return res.redirect("/account"); });
this setting author users id , guess right. 1 question here is, have update collection in user-model well? guess not not sure.
i fetching random events query:
event.find().limit(5).exec(function(err, events) { return res.view({ events: events }); });
and want users name in ejs-template this:
<% for( var i=0; < events.length; i++) { %> <li> <h3><%- events[i].author.name %></h3> </li> <% } %>
but not work. think doing wrong collection-field dont know what. suggestions?
i found answer. added populateall query doing join thing.
event.find().limit(5).populateall().exec(function(err, events) { return res.view({ events: events }); });
Comments
Post a Comment