相关疑难解决方法(0)

在猫鼬中将元素推送到数组

我正在尝试将元素推送到 mongoose 中的数组。我正在用更新和 $push 来做。但它没有在数据库中更新它。这是我的代码。路线.js:

    var Chooser = require('../chooser');

    var appRouter = function(app) {

    app.put("/chooser/:id", function(req, res) {
    console.log("ID: ", req.params.id);
    if (!req.body.toFollow) {
        return res.send({"status": "error", "message": "The account that you want to follow is needed"});
    }
    else {
        console.log("Cuenta: ", req.body.toFollow);
        Chooser.update({_id: req.params.id}, {$push: {accounts: {"name": "foo", "idAccount": 123456}}});
        return res.send({"status": "ok"});
    }

  });
}
Run Code Online (Sandbox Code Playgroud)

这是我的猫鼬模式。选择器.js:

var mongoose = require('mongoose');

var chooserSchema = mongoose.Schema({
_id: Number,
accounts: [{name: String, idAccount: Number}]
}, { _id: false });

var Chooser …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js mean-stack

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×1

mean-stack ×1

mongodb ×1

mongoose ×1

node.js ×1