简体   繁体   中英

Mongoose error populating virtual

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

Went back through all the code there again and decided to roll back changes it seems that I was 4 updates behind mongoose and in version 4.6.7 they added

fix(populate): throw more descriptive error when trying to populate a virtual that doesn't have proper options #4602

I never catered for this due to on version 4.6.4 as it gave me the results I wanted I suspect that my function above is now outdated on versions higher than 4.6.4. The moral of this answer check the package.json ensure that if this or something like this happens to you that you ensure you dont have npm setup with "^" before your package as this will update it when a suitable updated release is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM