【问题标题】:Add new item to existing array in mongoose将新项目添加到猫鼬中的现有数组
【发布时间】:2017-12-21 00:50:32
【问题描述】:

我在数据库 e.i. 中有带有描述产品的数组。 [aaa, bbb, ccc] 我想像 [aaa, bbb, ccc, ddd] 一样将新项目添加到我的数组中,但是我的代码没有将新项目添加到数组中,只是更改了现有的数组,例如 [ddd]。 问题:我应该在我的代码中更改什么以将新项目添加到现有数组?

index.html

<form name="editProduct.addForm" ng-submit="editProduct.addDescription(addDescription, editProduct.addForm.addDescription.$valid)" novalidate>
<div ng-class="{ 'has-success':(editProduct.addForm.addDescription.$valid && !editProduct.addForm.addDescription.$pristine), 'has-error':(!editProduct.addForm.addDescription.$valid && !editProduct.addForm.addDescription.$pristine) || (!addForm.addDescription.$valid && addForm.$submitted) }">
    <label><h4>Add description:</h4></label>
    <strong><input class="form-control" type="text" name="addDescription" ng-model="addDescription" placeholder="Add description" required></strong>
</div>
<br>
<button ng-disabled="editProduct.disabled" class="btn btn-primary add-des" type="submit">Save</button>
</form>  

api.js

 router.put('/editProduct', function(req, res){
    var editProduct = req.body._id;
    if(req.body.addDescription) var addDescription = req.body.addDescription;

    User.findOne({ username: req.decoded.username }, function(err, mainUser) {
        if(err) throw err;
        if(!mainUser) {
            res.json({ success: false, message: 'User no found'});
        } else {
            if(addDescription){
                if(mainUser.permission === 'admin') {
                    var options = { new: true };
                    Product.findOneAndUpdate({ _id: editProduct }, { description: addDescription }, options, function(err, product){
                        if(err) throw err;
                        if(!product){
                            res.json({ success: false, message: 'Product no found' });
                        } else {
                            product.update(function(err){
                                if(err){
                                    console.log(err);
                                } else {
                                    res.json({ success: true, message: 'Added new description'})
                                }
                            });
                        }
                    });
                } else {
                    res.json({ success: false, message: 'You are not an admin'})
                }
            }
        }
    })
})

【问题讨论】:

    标签: javascript angularjs node.js mongoose


    【解决方案1】:

    您可以使用 $push 运算符将新项目推送到这样的数组 -

    Product.findOneAndUpdate(
    { _id: editProduct }, 
    { $push: {
        description: addDescription
    }}, options, function(err, product){
    
    });
    

    【讨论】:

    • 谢谢,效果很好,但如果我有一个项目,这是一个对象并且 $push 不起作用,但可以将 ogject 更改为数组?
    • 如果你想一次推送多个描述,试试这个 - ``
    • 如果你想一次推送多个描述,试试这个 - ` Product.findOneAndUpdate( { _id: editProduct }, { $push: { description: { $each: descriptionList } }}, options,功能(错误,产品){}); `
    • 非常感谢您的回答,您的解决方案效果很好,但我有最后一个问题,您可以看看stackoverflow.com/questions/45163398/… 这个问题吗?我不知道如何在点击时删除此项目,
    猜你喜欢
    • 2019-11-30
    • 2019-09-11
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2017-12-22
    • 2018-08-11
    相关资源
    最近更新 更多