【发布时间】:2013-06-10 00:13:14
【问题描述】:
我是 mongodb 的新手,在过去的几天里,我一直在尝试将我的条目放入我的 mongolab 实例中,但没有任何运气。似乎当我执行保存调用时,我收到一条错误消息:
TypeError: Cannot use 'in' operator to search for '_id' in [object Object]
他们所指的 [object Object] 是我的颜色模式。我还没有找到答案,我想我会在这里发帖,以便在我进行更多研究的同时并行工作。我已经粘贴了我正在使用的内容的片段,希望这只是我正在做的一些愚蠢的事情。蒂亚!
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");
}
});
};
【问题讨论】:
-
你确定 req.query.path 返回一个有效值吗?输入
console.log(req.query.path)以确保值符合您的预期 -
是的,它是我的图像的路径。抱歉,我删除了所有的 console.logs 以缩小帖子的内容。也许我应该把它们留在:) 需要注意的是,它在第一个错误块中没有错误。当我调用 img.save 时,它在错误块中出错。但是,在您发表评论后,我去检查了颜色对象的内容,我的值非常奇怪。我认为这可能是问题所在。我认为当我对传入的字符串进行转义时,它会弄乱一些东西。必须马上去开会。当我回来时会检查。谢谢!