我的mongoose架构中有这个......和一些小组......
'use strict';
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var clientSchema = new mongoose.Schema({
name : { type: String },
offerings : [{ type: String }],
cscPersonnel : {
salesExec : { type: Schema.Types.ObjectId, ref: 'User' },
accountGM : { type: Schema.Types.ObjectId, ref: 'User' },
},
},
netPromoterScore : { type: Number }
});
module.exports = mongoose.model('clients', clientSchema);
我试图填充reff dis方式...我也填充了ref(用户为{path:'cscPersonnel'})
function getOneById(id){
var deferred = Q.defer();
console.log("im in get by id" +id);
model
.findOne({ _id: id })
.populate({path:'cscPersonnel'})//one way
/* 'cscPersonnel salesExec', //second way
'cscPersonnel accountGM', */
.exec(function (err, item) {
if(err) {
console.log(err);
deferred.reject(err);
}
else
console.log(item);
deferred.resolve(item);
});
return deferred.promise;
} // gentOneById method ends
但不幸的是最终出现了这个错误!!!!
CastError:对于路径“_id”的值“[object Object]”,Cast to ObjectId失败
{
"message": "Cast to ObjectId failed for value \"[object Object]\" at path \"_id\"",
"name": "CastError",
"type": "ObjectId",
"value": {
"salesExec": "56cf5f09245f8a240b30693b",
"accountGM": "56cf5f09245f8a240b30693b"
},
"path": "_id"
}
如何让它解决这个问题.... 帮助,提前谢谢
答案 0 :(得分:1)
请尝试这个
model
.findOne({ _id: id })
.populate({path: 'cscPersonnel.salesExec'})
.populate({path: 'cscPersonnel.accountGM'})
.exec(function (err, item) {