首页 > 解决方案 > Mongoose“query.save()”不更新布尔值

问题描述

我正在尝试创建一个 JSON-api 来管理待办事项。当用户使用 post 请求创建文档时,服务器返回一个令牌和创建的文档的id 本项目使用mongodb保存数据,并使用mongoose发出请求。

这是一个测试应用程序,我想练习自己在文档名称待办事项令牌中使用加密。

现在我处于有选项的情况,我需要将protectedWithToken值保存在设置对象中。它似乎不起作用,所以我添加了几个console.log()函数。

这是 mongodb 集合的架构:

架构

const toDoSchema = mongoose.Schema({
    "name": {
        type: String
    },
    "todo": {
        type: String,
        required: true
    },
    "iv": {
        type: String
    },
    "secretToken": {
        type: String,
        required: true,
        unique: true
    },
    "options": {
        type: Object,
        required: true
    }
}, { collection: config.collectionName });

保存我的文档的代码

        console.log('fast comparison before validation; wants',mem.options.protectedWithToken , 'already is',query.options.protectedWithToken)
        if (query.options.protectedWithToken === true || mem.options.protectedWithToken !== query.options.protectedWithToken) {

            // user didn't give token, and the document is protected
            if (!request.token) {
                // ......
            }

            // compare token values.
            
            if (request.token !== tokencontent) {
                // ....
            }
        }

        try {
            // save protectedWithToken to database

            console.log('before query.... = ...',query.options)

            query.options.protectedWithToken = mem.options.protectedWithToken
            console.log('after query.... = ...','database options',query.options, 'request options',mem.options)

            // save document
            updatequery = await query.save();
            console.log('saved:', updatequery.options.protectedWithToken)

            // declare response body
            result.status = 200
            result.id = query._id
            result.error = "document saved"
            
            // send response
            res.status(result.status).json(result)

一切似乎都很好......除了一件小事。它不会将 options.protectedWithToken 更新到数据库。'已保存:',更新查询返回选项

程序返回什么

我发送了一个"options": { "protectedWithToken": true }请求。使用正确的令牌,服务器没有返回错误。


fast comparison before validation; wants true already is false
before query.... = ... (db) { protectedWithToken: false }
after query.... = ... (db) { protectedWithToken: true } (request) { protectedWithToken: true }
saved: true

如您所见,已保存为真,这意味着应该保存在数据库中。我查看了 中的选项cloud.mongodb.com。他们说 options.protectedWithToken 是假的。

更新/修复

通过更改"options": {type: Object}to"options": {type: String}并在插入数据库之前将对象字符串化为 json 来使其工作。

标签: node.jsmongodbmongooseboolean

解决方案


Schema.Types.Mixed

我在您的架构中看到的一个问题是Object您尝试设置的类型options。据我所知,文档没有Object列为有效的 SchemaType。(https://mongoosejs.com/docs/schematypes.html

我想你正在寻找的是Schema.Types.Mixed,所以像这样

options : {
  type: Schema.Types.Mixed,
  required: true
}

或喜欢其中之一

options : Object
options : {}
options : mongoose.Mixed

注意(来自文档):

由于 Mixed 是一种无模式类型,您可以将值更改为您喜欢的任何其他值,但 Mongoose 失去了自动检测和保存这些更改的能力。要告诉 Mongoose Mixed 类型的值已更改,您需要调用 doc.markModified(path),将路径传递给刚刚更改的 Mixed 类型。

也许这已经解决了你的问题


推荐阅读