gpt4 book ai didi

node.js - Mongoose 不会让我为数字插入空值吗?

转载 作者:可可西里 更新时间:2023-11-01 10:04:31 24 4
gpt4 key购买 nike

我定义了以下 Mongoose 模式:

participantSchema = new Schema({
id: Number,
email: String,
survey: {
type: Schema.Types.ObjectId,
ref: 'Survey'
},
created: {
type: Date,
"default": Date.now
},
answers: [
{
question: {
type: Schema.Types.ObjectId,
ref: 'Question'
},
values: Array,
question_id: Number,
sub_id: Number
}
]
});

当我尝试为我的 answers 数组插入以下值时:

[{ question_id: 60800,
_id: 52f53345f06cf301f2a38465,
values: [ 'c2' ] }
{ question_id: 60801,
sub_id: 19690,
_id: 52f53345f06cf301f2a38464,
values: [ 'C1' ] }
{ question_id: 60801,
sub_id: 19691,
_id: 52f53345f06cf301f2a38463,
values: [ 'C3' ] }
{ question_id: 60802,
_id: 52f53345f06cf301f2a38462,
values: [ null ] }
{ question_id: 60803,
sub_id: 19692,
_id: 52f53345f06cf301f2a38461,
values: [ null ] }]

它抛出错误:

CastError: Cast to number failed for value "null" at path "sub_id"

如果我将所有 sub_id 硬编码为 1 或 null 它似乎可以工作,但是当存在混合时它会抛出错误。有任何解决方法的建议吗?

最佳答案

这样做:

sub_id: {type: Number, sparse: true}

并查看 this

关于node.js - Mongoose 不会让我为数字插入空值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21636282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com