2

私はmongodbを初めて使用し、過去数日間、mongolabインスタンスにエントリを取得しようとしましたが、運がありませんでした. 保存呼び出しを実行すると、次のエラーが表示されるようです。

TypeError: Cannot use 'in' operator to search for '_id' in [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");
        }
    });
};
4

1 に答える 1

2

したがって、リクエストが処理されているときにカラーオブジェクトがエスケープされていなかったことが原因でした。入ると、オブジェクトが表示されましたが、ネストされた値が有効ではなかったため、文字列を期待していたため、dbへの書き込みがスローされていました。代わりに POST を実行し、json オブジェクトをデータ パラメータに渡し、それを body から読み戻すと、期待どおりに機能し、必要に応じてデータベースが自動的に作成されました。Noahさん、お返事ありがとうございます!

于 2013-06-13T18:41:52.800 に答える