【发布时间】:2018-08-07 13:42:44
【问题描述】:
所以基本上我有一个愿望清单,我有一堆产品,我想使用 put 请求将它们添加到愿望清单产品数组中(顺便说一句,我正在使用邮递员)。 这是愿望清单模式,是的,我知道数据库中的文档名称是“whishlist”....我讨厌拼写错误
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;
var whishList = new Schema({
title: {type: String, default: "Cool whishlist"},
products:[{type: ObjectId, ref:'Product'}]
});
module.exports = mongoose.model('WhishList', whishList);
这是产品架构
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var product = new Schema({
title: String,
price: Number,
likes: {type: Number, default: 0}
});
module.exports = mongoose.model('Product', product);
现在这是我要运行的代码
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/swag-shop');
var Product = require('./model/product');
var wishList = require('./model/wishlist');
app.put('/wishlist/product/add', function(request, response){
Product.find({_id: request.body.productId}, function(err, product){
if(err) {
response.status(500).send({err: "could not add item to wishlist"});
}else{
wishList.update({_id: request.body.wishlistId},{$addToSet: {products: product._id}}, function(err, wishlist){
if(err){
response.status(500).send({err: "could not add item to wishlist /update/"});
}else{
response.send(wishlist);
}
});
}
});
我真的看不出问题出在哪里,我尝试删除文档并再次发布,但我遇到了同样的问题。 提前致谢
【问题讨论】:
标签: javascript node.js mongodb mongoose mongodb-query