Note that there are some explanatory texts on larger screens.

plurals
  1. POrender view in SailsJS after mongodb forEach loop query
    primarykey
    data
    text
    <p>I have 2 collections name "Keywords" and "Company", I use MongoDB aggregate framework to retrieve related object._id from "Keywords" collection base on keywords user key in. </p> <p>After i got the object._id from keywords collection, i want to query and compile a final complete docs from Company collection using the object._id.</p> <p>I stuck on the part where res.view() run first before the result[] collect all the docs from Company collection.</p> <p>I need to help to turn my code to Synchronous approach. Please help me. Below are what i did.</p> <p>Sample of doc from "Keywords" Collection</p> <pre><code>{ "_id": ObjectID("52ac7130bd40d00a7beb7a29"), "keyword": "Sunshine", "object": [ { "_id": ObjectID("528443ce751fc9b805d640ad"), "type": "companyName" } ] } </code></pre> <p>Sample of doc from "Company" Collection</p> <pre><code>{ "_id": ObjectID("528443ce751fc9b805d640ad"), "name": "Sunshine Plaza", ... ... ... } </code></pre> <p>SearchController in SailsJS</p> <pre><code>var keyWords = req.query.q, searchKeywords = keyWords.toLowerCase().replace(/[^\w\s]/gi, ' ').split(' '), //For example user key in "Sunshine Plaza" results = []; Keyword.native(function(err,collection){ collection.aggregate([ { $project : { '_id' : 0, 'keyword' : 1, 'object' : 1 } }, { $match : { 'keyword' : { '$in' : searchKeywords } } } , { $unwind : '$object' } , { $group : { _id : '$object._id', count : { $sum : 1 } } } , { $sort : { count: -1 } } , { $skip : 0 } , { $limit : 10 } ], function (err, docs){ docs.forEach(function (doc, i){ Company.findOne({ '_id' : doc._id },function(err,docs){ results.push(docs); }); }); }); }); res.view({ key : keyWords, results : results, layout: "layouts/search" }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload