【发布时间】:2020-04-15 11:22:24
【问题描述】:
我有示例输入
[[1,2],[3,2],[1,3],...,[4,5]]
如何在猫鼬中编写模型架构?
这是我的架构
const SubproductSchema = new Schema({
...
positions: [{
type: [Number],
validate: {
validator: function(value){
return value.length == 2;
},
message: 'Positions should be 2'
}
}]
}, {
timestamps: true
});
这不起作用。
输入应该是固定大小的数组,数组长度为 2,像这样[[1,2],[3,2],[1,3],...,[4,5]]
如果输入为[[1,2,4],[3,2],[1,3],...,[4,5]],则应使用'Position should be 2' 进行验证
更新
我也试过这段代码(我猜逻辑上是正确的):
const SubproductSchema = new Schema({
...
positions: {
type: [{
type: [Number],
validate: {
validator: function(value){
return value.length == 2;
},
message: 'Positions should be 2'
}
}],
}
}, {
timestamps: true
});
我的帖子是
{
...
"positions": [[2,3], [1,4], [4, 5]]
}
它输出错误:
子产品验证失败:职位:转换为数组失败 路径 \"positions\"" 处的值 \"[ [ [ 2, 3 ], [ 1, 4 ], [ 4, 5 ] ]\"
【问题讨论】:
-
@GeorgeBailey 我想这不是我的问题...解决了不是我的问题...我也尝试过索引 2d 或 2dsphere .. 问题是当我用 [[1, 2],[3,2],[1,3],...,[4,5]] 是错误验证失败:位置:Cast to Array failed for value \"[ [ 2, 3 ], [ 1, 4 ], [ 5, 6 ]\" at path \"positions\... 我不知道为什么我不能验证内部数组
标签: javascript node.js mongoose mongoose-schema