【问题标题】:$addToSet to an array but it gives me null$addToSet 到一个数组,但它给了我 null
【发布时间】: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


    【解决方案1】:

    问题是Product.find() 的结果是一个 Mongoose 文档数组,如果查询匹配集合中的任何文档而不是您想要的单个文档。

    因此表达式{$addToSet: {products: product._id}} 解析为{$addToSet: {products: undefined}},因为product 是一个数组,而product._id 是未定义的。举个简单的例子

    var product = [{ '_id': 1 }];
    console.log(product._id) // logs undefined
    

    要解决这个问题,您可以访问数组中的唯一元素

    wishList.update(
        { '_id': request.body.wishlistId },
        { '$addToSet': { 'products': product[0]._id} }, 
        function(err, wishlist) { ... }
    );
    

    或者使用findOne()查询产品时返回单个文档的方法:

    Product.findOne({ '_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) { ... }
            );
        }
    });
    

    findById() 方法在这种情况下也很有用,即

    Product.findById(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) { ... }
            );
        }
    });
    

    【讨论】:

    • 它有效,但为了确保我理解,这意味着find 函数只给了我一组文档,而findOne 只给了一个文档?但我仍然无法真正理解为什么它给出 null ,比如如何以及为什么?
    猜你喜欢
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2023-01-30
    • 2019-10-15
    • 2017-03-26
    相关资源
    最近更新 更多