简体   繁体   English

用猫鼬将新项目添加到现有数组

[英]Add new item to existing array in mongoose

I have array with description product in database ei [aaa, bbb, ccc] and I would like add new item to my array like [aaa, bbb, ccc, ddd], but my code not adding new item to array just changing existing array like [ddd]. 我在数据库ei [aaa,bbb,ccc]中有带有描述产品的数组,并且我想像[aaa,bbb,ccc,ddd]一样将新项目添加到我的数组中,但是我的代码没有将新项目添加到数组中,只是更改了现有数组像[ddd]。 Question: What I should change in my code to adding new item to existing array? 问题:我应该在代码中进行哪些更改以向现有数组添加新项?

index.html 的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 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'})
                }
            }
        }
    })
})

You can use $push operator for pushing a new Item to an array like this - 您可以使用$ push运算符将新的项目推入这样的数组中-

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM