Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何保存嵌入的文档而不出错;密钥$不能以'$';"(x2026)";在使用devtool和mongoose4.x时_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何保存嵌入的文档而不出错;密钥$不能以'$';"(x2026)";在使用devtool和mongoose4.x时

Node.js 如何保存嵌入的文档而不出错;密钥$不能以'$';"(x2026)";在使用devtool和mongoose4.x时,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在从mongoose 3.8.22升级到4.x 在我开始调试devtool的一个问题之前,一切似乎都运行得很好。在使用mongoose 3.8.22时,我已经使用了一个月,没有任何问题 在运行我的节点服务器时,我没有遇到任何错误或问题。但是,在使用npm软件包“devtool”运行我的服务器时,包含子文档的任何保存都会失败,并出现以下错误: “键$不能以“$”(…)开头。” 是否有人遇到过此问题或找到了解决方法 例如: 我从两个模式开始,person和customOpts var Custo

我正在从mongoose 3.8.22升级到4.x 在我开始调试devtool的一个问题之前,一切似乎都运行得很好。在使用mongoose 3.8.22时,我已经使用了一个月,没有任何问题

在运行我的节点服务器时,我没有遇到任何错误或问题。但是,在使用npm软件包“devtool”运行我的服务器时,包含子文档的任何保存都会失败,并出现以下错误:

“键$不能以“$”(…)开头。”

是否有人遇到过此问题或找到了解决方法

例如:

我从两个模式开始,person和customOpts

var CustomOptions = new Schema({
    optsType: {
        type: String,
        required: true
    },
    someOption: {
        type: Boolean,
        required: true
    }
});
var Person = new Schema({
    name: {
        type: String,
        required: true
    },
    customOpts: [CustomOptions]
});
接下来,我创建x个新的CustomOpt并将它们添加到一个新的person

var newOpts = new locator.db.customOptions({
    optsType: 'some_opts',
    someOption: true,
});

var newPerson = new locator.db.person({
    name: 'Steve',
    customOpts: [newOpts]
});

使用newPerson.save()时,我在节点上没有问题,但只要我在customOpts数组中有newOpts,devtool就会抛出错误。

您是否有使用
CustomOptions
schema定义的模型?是的,使用db.connection。一旦我们设置了db.CustomOptions=db.connection.model('CustomOptions',TestSchemas.CustomOptions);您是否尝试过将
customOpts
数组保存为对象数组,而不是将Mongoose文档保存为
var newOpts={optsttype:'some_opts',someOption:true};var newPerson=newlocator.db.person({name:'Steve',customOpts:[newOpts]})?当在文档上强制toObject时,它将起作用,但是这是一个非常小的示例来演示这个问题。应用程序要复杂得多,因此需要重写/测试以确保正确性,尽管这可能是唯一的选择,因为您已在
Person
模式中将
customOpts
字段定义为
[CustomOptions]
模式的数组,最合乎逻辑的做法是向数组中添加具有相同
CustomOptions
schema的对象。另一个选项是将
customOpts
作为选项模型的引用数组,作为
customOpts:[{type:Schema.Types.ObjectId,ref:'CustomOptions'}]
的一个数组,您可以将数组另存为
\u id
s。您有使用
CustomOptions
Schema定义的模型吗?是,使用db.connection.once设置db.customOptions=db.connection.model('customOptions',TestSchemas.customOptions);您是否尝试过将
customOpts
数组保存为对象数组,而不是将Mongoose文档保存为
var newOpts={optsttype:'some_opts',someOption:true};var newPerson=newlocator.db.person({name:'Steve',customOpts:[newOpts]})?当在文档上强制toObject时,它将起作用,但是这是一个非常小的示例来演示这个问题。应用程序要复杂得多,因此需要重写/测试以确保正确性,尽管这可能是唯一的选择,因为您已在
Person
模式中将
customOpts
字段定义为
[CustomOptions]
模式的数组,最合乎逻辑的做法是向数组中添加具有相同
CustomOptions
schema的对象。另一个选项是将
customOpts
作为对选项模型的引用数组,作为
customOpts:[{type:Schema.Types.ObjectId,ref:'CustomOptions'}]
在这里可以将数组另存为
\u id
s。