Mongoose TypeError:无法使用'in'运算符在[object Object]中搜索'_id' [英] Mongoose TypeError: Cannot use 'in' operator to search for '_id' in [object Object]

查看:84
本文介绍了Mongoose TypeError:无法使用'in'运算符在[object Object]中搜索'_id'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb的新手,在过去的几天里,我一直在尝试将自己的条目加入mongolab实例,但没有任何运气.似乎在执行save调用时出现错误提示:

I am new to mongodb and for the past couple days I have been trying to get my entries into my mongolab instance but have not had any luck. It seems when I execute the save call I get an error stating:

TypeError: Cannot use 'in' operator to search for '_id' in [object Object]

他们引用的[object Object]是我的Color模式.我还没有找到答案,并想在我进行更多研究的同时发贴到这里并行工作.我贴了我正在使用的东西的小贴士,希望这只是我正在做的愚蠢的事情. TIA!

The [object Object] they are referring to is my Color schema. I havent been able to find an answer yet and thought I would post here to work in parallel while I research more. I have pasted a snipit of what I am using in hopes that it is just something stupid I am doing. TIA!

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");
        }
    });
};

推荐答案

所以这是由于处理请求时未释放我的颜色对象的方式.进入后,它看到了对象,但是嵌套的值无效,因此正像期望的那样抛出了对db的写操作.我最终改为执行POST,并在数据参数中传递json对象,然后通过正文将其读回,它按预期工作,并根据需要自动创建了db.谢谢诺亚的回应!

So it was due to the way my color object was being unescaped when the request was being handled. Upon entering it was seeing the object but the nested values were not valid and therfore was throwing off the write tot eh db as it was expecting Strings. I ended up doing a POST instead and passing the json object in the data param and then reading it back out through the body and it works as expected and has automatically created the db as desired. Thanks Noah for the response!

这篇关于Mongoose TypeError:无法使用'in'运算符在[object Object]中搜索'_id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆