I'm working on a project using node and mongo. I have it locally and 'live' on a server for testing. The local copy of the project has no issues at all but the live version crashes with an error:
/root/farm_api/node_modules/mongoose/lib/model.js:3002 throw new Error('If you are populating a virtual, you must set the ' + ^
Error: If you are populating a virtual, you must set the localField and foreignField options at getModelsMapForPopulate (/root/farm_api/node_modules/mongoose/lib/model.js:3002:13) at populate (/root/farm_api/node_modules/mongoose/lib/model.js:2647:15) at _populate (/root/farm_api/node_modules/mongoose/lib/model.js:2615:5) at Function.Model.populate (/root/farm_api/node_modules/mongoose/lib/model.js:2575:5) at Immediate. (/root/farm_api/node_modules/mongoose/lib/query.js:1276:17) at Immediate._onImmediate (/root/farm_api/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16) at processImmediate [as _immediateCallback] (timers.js:383:17)
I dont know what the issue is I think its to do with a this api call where I use aggregate
and $lookup
Can be seen here:
api.dbm.Field.aggregate([
{
$lookup: {
from: 'tasks',
localField: '_id',
foreignField: 'field',
as: 'fieldTask'
}
},
{
$lookup: {
from: 'stocks',
localField: 'owner',
foreignField: 'owner',
as: 'stock'
}
}
], function(err, result){
if(err) console.log(err);
else {
var aggregatedData = result;
api.checkStaffRole(req.query.as, req.user._id, req.query.fid, res, function(fields, ids)
{
aggregatedData.forEach(function(record, index, object){
if(!api.lodash.find(fields, {'_id': record._id})){
object.splice(index, 1);
}
});
api.lodash.forEachRight(aggregatedData, function(record, index, object){
if(!api.lodash.find(record.staff, {'user': req.user._id})){
object.splice(index, 1);
}
});
res.json(api.buildResponse(null, aggregatedData));
});
}
});
I think its because their is no records in the collections at the time of calling the api but why does it work locally then and only have an issue when its on a server?
Both servers have mongo 3.2 installed and in npm I use mongoose 4.6.8 on both copies (was using 4.6.4 on local with no issue uppdated the package and checked still no issues).
Update: Re-installed mongodb and mongoose via npm both versions now have the exact same packages(due to me using the ^
in package.json) and I still cant replicate the error on local host.
Altered code on the server to print out the result value and it displays on console so it aggregates the data then crashes immediately after executing