我是 mongodb 的新手,在过去的几天里,我一直试图让我的条目进入我的 mongolab 实例,但没有任何运气。似乎当我执行保存调用时,我收到一条错误消息:
TypeError: Cannot use 'in' operator to search for '_id' in [object Object]
他们所指的 [object Object] 是我的 Color 模式。我还没有找到答案,我想我会在这里发帖,以便在我进行更多研究的同时并行工作。我已经粘贴了我正在使用的内容的片段,希望这只是我正在做的一些愚蠢的事情。蒂亚!
mongoose.connect(config.db.mongodb);
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Color = new Schema({
hex : {type:String, required: true, trim: true}
,perc : {type:String, required: true, trim: true}
});
var Img = new Schema({
path : {type:String, required: true, trim: true}
,color : [Color]
});
var imgModel = mongoose.model('Img', Img);
exports.addImage = function(req,res){
//First check to see if we already have the image stored
imgModel.findOne({path: req.query.path}, function(error, image){
if(error){
console.log("Error");
res.json(error);
}
else if(image === null){
//There is no image so just store the info
var image_data = {
path: req.query.path,
color: req.query.color
};
var img = new imgModel(image_data);
img.save(function(error, data){
//*** This error block below is where I am
// entering and the message is displayed
if(error){
console.log("Oh noo: ",error);
res.json(error);
}
else{
console.log("Saving: ",data);
res.json(data);
}
});
} else{
//The path is already here
//res.json("Image already in db");
console.log("Image already in db");
}
});
};