2016-12-19 17 views
1

こんにちは私はコレクションにドキュメントを追加しようとしていますが、追加していないので、私はそれを検証するRobomongoのJSONを検証しました。私はそれがうまく動作する他のモデルと同じことをやったが、何らかの理由でそれはここで働いていない誰もがエラーが何であるか見ることができます。モンゴース作成オブジェクトが動作しない

exports.add_ads = function(req, res) { 
if (Object.keys(req.body).length == 0 || 
    req.body.user_id == undefined || req.body.user_id == "" || 
    req.body.rate == undefined || req.body.rate == "" || 
    req.body.ads.type == undefined || req.body.ads.type == "") { 
    res.status(404).send({ error: "One or more request peremeter is empty" }); 
} 
// console.log(req.body.ads['type']); 
new Review({ 
    user_id: "58492c6f05c095160e37436c", 
    ads: { 
     type: "view" 
    }, 
    product: { 
     product_id: "", 
     review: "", 
     rating: 0 
    }, 
    rate: 0.07, 
    isActive: true 
}).save(); 
res.end(); 

}

私は製品や広告を削除した場合、機能が正常に動作します。

モデル:adとして

ads: { 
    type: { type: String } 
}, 

が予約されている 'タイプ' と呼ばれるプロパティがあり

ads: { 
    type: String 
}, 

スキーマ定義で
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var User = require('../models/user.js'); 
var Product = require('../models/product.js'); 

// define model ========================================================================= 
var ReviewSchema = new Schema({ 
    user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, 
    ads: { 
     type: String 
    }, 
    product: { 
     product_id: { type: mongoose.Schema.Types.ObjectId, ref:  'Product'}, 
     review: String, 
     rating: Number 
    }, 
    rate: Number, 
    isActive: Boolean, 
    dateCreated: Date 
}); 

module.exports = mongoose.model('Review', ReviewSchema); 
+0

'Review'モデルのスキーマをどのように定義したかを示すことができますか? – chridam

+0

@chridam私の投稿をモデル –

+0

で更新しましたスキム定義で、この広告を 'ads:{ type:String }、' '広告:{ タイプ:{type:String} }と変更しました。 'ad'には 'type'というプロパティがあり、これはマングースタイプのために予約されています。また、モデルは空の文字列ではなく、 'product_id'のための有効な' ObjectId'文字列表現を期待しています。 – chridam

答えて

0

、この行を変更マングースタイプの場合。

また、このモデルでは、product_idの有効なObjectId文字列表現が必要であり、空の文字列ではありません。

関連する問題