To get this to work, you have two alternatives:
Using Sails.js version 0.9
You have to nest the queries. Take a look at this example:
In your .done()
function:
.done(function foundPosts(err, posts) {
posts.forEach(function(post){
User.find().where({ owner: post.ownerID }).done(function (err, user) {
// create new property on the post object
post.user = user.ownerName;
});
});
res.view({
posts: posts,
layout: req.session.layout
});
}
Using Sails.js version 0.10
In the (currently) latest version of sails.js you have associations. That means you can model the association in you model schema. Example:
// From the file: myApp/api/models/posts.js
module.exports = {
attributes: {
title:'STRING',
body:'STRING',
author:{
model: 'user' // Here you associate the user model
}
}
}
That means, in your query, you could do something like this:
Post.find().where({ customerID: customerID }).populate('user').exec(function(err, posts){
res.view({
posts: posts,
layout: req.session.layout
});
});