【问题标题】:Mongoose not $setting array.$.booleanValue when I findOneAndUpdate({paramOne: p, paramTwo, array.keyName: valueName})猫鼬不是 $setting array.$.booleanValue 当我 findOneAndUpdate({paramOne: p, paramTwo, array.keyName: valueName})
【发布时间】:2018-01-04 00:01:42
【问题描述】:

我正在尝试通过

 app.post("/api/post/:chorecomplete", function(req, res){
            Parent.findOneAndUpdate({parentFirstName: req.body.parentFirstName, parentLastName: req.body.parentLastName, "chores.choreName": "firstChore"}, {$set: {"chores.$.complete": "true"}}).exec(function(err, doc){
                if (err){ console.log(err); res.send("not ok");}
                    else{
                        console.log(doc);`enter code here`
                        res.send(ok);
                    }
            })
        })

    }

我的服务器记录了猫鼬的响应,但不是因为我也在告诉它。我注释掉了 console.log(doc) 并对其进行了测试以查看我的数据库是否正在返回它或者我的服务器是否正在记录它。事情是,我的服务器以“Ok”响应,这意味着猫鼬没有抛出错误,但它没有修改我的 POST /api/post/chorecomplete 200 17.378 ms - 2 [1] { _id: 597aa48313cf6914905916e0, [1] parentFirstName: 'firstName', [1] parentLastName: 'lastName', [1] parentEmail: 'mich@ael.com', [1] __v: 0, [1] children: [], [1] chores: [1] [ { choreName: 'firstChore', [1] choreDesc: 'firstDescription', [1] value: null, [1] complete: false }, [1] { choreName: 'secondChore', [1] choreDesc: 'secondDescription', [1] choreValue: null, [1] complete: false }] }

我不知道我做错了什么。我在那里有一个静态查询参数,但“firstChore”最终将是 req.body.choreName。我开始怀疑我是否应该为家务制作一个集合并将其引用到我的父模式和子模式,但我不是 100% 确定如何去实现对父和子的引用,这将相互引用.. . (Parent 和 Child 都有单独的用户帐户,Child 将更新要完成的家务活,并将收到 choreValue [在本例中应该是 $5.00,但由于某种原因 mongoose 不接受它。目前不是除了表单值之外的任何地方定义。])。这是一个示例模式。 (我试图尽可能详细。已经很晚了,这是我在这里的第一个问题。)

```//父模式 var 猫鼬 = 要求(“猫鼬”); var Schema = mongoose.Schema;

var ParentSchema = new Schema({ 父母名字:{ 类型:字符串, 要求:真 },

parentLastName: {
    type: String,
    required: true
},

parentEmail: {
    type: String,
    required: true,
    validate: [
    function(input){
        input.length >= 3;
    },
    "Email must be a valid email"
    ]
},

password: {
type: String,
//required: true,
validate: [
    function(input){
        input.length >= 6;
    },
    "Password must be at least 6 characters"
    ]
},

chores: { 
    type: Array,
},

children: [{
    type: Schema.Types.ObjectId,
    ref: "Child"
}]

});

var Parent = mongoose.model("Parent", ParentSchema);

module.exports = 父级;

```

这是向数组添加杂项的路径。它将是表单值。在这条路线中有效地定义了一项杂务,而不是明确定义为它自己的模型。 //route for inserting chores app.post("/api/post/:chores", function(req, res){ //When we have someone logged in we will take one of the values we get from their presence, (either _id or email) and replace my name. It's only my name b/c it was the name I initially inserted into the db. //if chores === chores then findAll else if {var theChoreToFind === req.params.chores} and we'll run that chore to update a chore? var parentFirstName = req.body.parentFirstName; var parentLastName = req.body.parentLastName; var choreName = req.body.choreName; var choreDesc = req.body.choreDesc; var choreValue = req.body.choreValue; Parent.findOneAndUpdate({parentFirstName: req.body.parentFirstName, parentLastName: req.body.parentLastName}, {$push: {chores: {choreName: choreName, choreDesc: choreDesc, choreValue: choreValue, complete: false}}}).exec(function(err, doc){ if(err) {console.log(err)} console.log(doc); }) res.send("Ok"); })//END new Chores

这是令人难以置信的长篇大论。我只是不想让我提出的任何东西不在这里供参考。如果你已经做到了这一步。谢谢。

【问题讨论】:

    标签: express mongoose postman


    【解决方案1】:

    可能的问题

    这看起来可能是您的 Express 路由而不是 MongoDB 的问题。

    您的代码的其余部分看起来有效,但您有一个名为 /api/post/:chorespost 路由和另一个名为 /api/post/:chorecomplete 的路由

    虽然参数名称不同,但 Express 实际上会将这些路由解释为相同,因为冒号使参数成为通配符,可以是您传递给它的任何值。

    在 Express 中,属性附加到 app 的顺序非常重要。

    在这种情况下,假设您没有任何其他与/api/post/:parameter 模式匹配的post 路由,如果/api/post/:chores 写在/api/post/:chorecomplete 之前,那么对/api/post/:chorecomplete 的任何发布请求实际上将匹配@987654330 @ 并运行您在该路径中的代码。

    这可能是您收到OK 响应的原因,而控制台是 从数据库中记录文档,因为您实际上是在发布到 /api/post/:chores 并运行成功的查询。

    解决方案

    有几种方法可以解决这个问题,所有这些都围绕着改变路线的路径。这完全取决于您的命名模式以及您计划如何访问数据。

    由于有问题的代码正在更新现有文档,您可以将其改为 put 路由

     app.put("/api/post/:chorecomplete", function(req, res) {...}
    

    或者您可以将其保留为 post 并将另一个名称附加到路径中,以便它不再匹配您代码中的另一个路由:

     app.post("/api/post/chorecomplete/:parameter", function(req, res) {...}
    

    【讨论】:

    • 我不敢相信我没有听懂……我认真地认为这与我的猫鼬语法有关,但这解释了它。谢谢!!
    猜你喜欢
    • 2020-11-13
    • 2013-02-13
    • 2019-05-23
    • 2021-09-25
    • 2017-10-05
    • 2015-09-15
    • 2016-06-20
    • 2018-07-15
    • 2013-06-19
    相关资源
    最近更新 更多