在其他集合中添加引用项目失败

时间:2017-03-11 09:44:13

标签: node.js mongoose mongoose-schema

foodtruck.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Items = require('./items.js');


var FoodTruckSchema = new Schema({
    foodtruck_name:String,
    foodtruck_location:String,
    foodtruck_rating:{type:Number,default:5},
    foodtruck_total_votes:{type:Number,default:0},
    foodtruck_tag:String,
    foodtruck_timing:String,
    foodtruck_cusine:String,
    foodtruck_img:String,
    foodtruck_logo:String,
    item_list: [ {type : mongoose.Schema.ObjectId, ref : 'items'}]
},{ versionKey: false });



module.exports = mongoose.model('foodtruck',FoodTruckSchema);

items.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ItemSchema = new Schema({
    no_of_times_ordered:Number,
    item_name:String,
    item_tag:String,
    item_category:String,
    item_illustrations:[String],
    item_stock:Number,   //0 available 1 last 5 items 2 not available
    item_quantity_ordered:{type:Number,default:0},
    item_price:Number,
    item_img:String,
    no_of_likes:{type:Number,default:0}
},{ versionKey: false });

module.exports = mongoose.model('items',ItemSchema);

不,我想添加新项目。添加了新项目,但不知何故,它的引用未传达给 foodtruck.item_list 。我正在尝试的查询如下:

var addItem = function(req, res) {
    var foodtruck_id = req.body.foodtruck_id;
    var newItem = new item();
    var itemList = [];
    newItem.item_name = req.body.item_name;
    newItem.item_tag = req.body.item_tag;
    newItem.item_category = req.body.item_category;

    for (var key in req.body) {
        if (req.body.hasOwnProperty(key)) {
            if (key == 'item_illustrations') {
                newItem.item_illustrations = req.body[key];
            }
        }
    }
    newItem.item_stock = req.body.item_status;
    newItem.item_price = req.body.item_price;

    if ((foodtruck_id) && (foodtruck_id.trim() != '')) {
        foodtruck.findById(foodtruck_id.trim(), function(err, foodtrucks) {
            if (err)
                res.json({
                    status: '500',
                    message: 'There is no data available'
                });

            newItem.save();
            foodtrucks.item_list.push(newItem);
            foodtrucks.save();
            console.log(foodtrucks);

            foodtruck.findById(foodtruck_id.trim()).populate('item_list').exec(function(err, foodtrucks) {
                if (err) res.json({
                    status: '500',
                    message: 'There is no data available'
                });
                res.send({
                    status: '200',
                    message: 'Review List',
                    data: foodtrucks
                });
            });
        });
    } else {
        res.json({
            status: '404',
            message: 'Please enter valid foodtruck id'
        });
    }

};

在这里,项目被添加,但foodtruck没有参考。那么,我该如何添加项目的引用?

foodtruck collection中新添加项目的屏幕截图

query image

1 个答案:

答案 0 :(得分:0)

您正在推送整个项目而不是新创建的项目的ObjectId。您需要保存新项目,然后在保存的回调中,您必须在_id中推送新创建的项目的item_list

而不是简单的保存试试这个:

//do everything inside the calllback of the save
newItem.save(function(err,savedItem){
    if(!err){
        foodtrucks.item_list.push(savedItem._id);
        foodtrucks.save();//youcan use callback here also to handle the errors
        console.log(foodtrucks);

        foodtruck.findById(foodtruck_id.trim()).populate('item_list').exec(function(err, foodtrucks) {
            if (err) res.json({
                status: '500',
                message: 'There is no data available'
            });
            res.send({
                status: '200',
                message: 'Review List',
                data: foodtrucks
            });
        });
    }else{
        res.json({
            status: '500',
            message: 'Error while saving new item'
        });
    }
});