Node.js-猫鼬路径验证失败-TypeError:无法调用未定义的方法"validate" [英] Node.js - Mongoose path validation failing - TypeError: Cannot call method 'validate' of undefined
本文介绍了Node.js-猫鼬路径验证失败-TypeError:无法调用未定义的方法"validate"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下Mongoose模式和路径验证:
I have the following Mongoose schema and path validaion:
var locationSchema = new Schema({
userid: { type: Number, required: true },
location: {
type: [{
type: "String",
required: true,
enum: ['Point', 'LineString', 'Polygon'],
default: 'Point'
}],
coordinates: { type: [Number], required:true }
},
tags: [{ type: String, index: true, required: true }],
create_date: { type: Date, default: Date.now }
});
locationSchema.path('location.coordinates').validate(function(coordinates){
return coordinates && coordinates.toString().match(/([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/g);
}, 'Invalid latitude or longitude.');
启动我的应用程序时,我得到:
When I start my app i get:
locationSchema.path('location.coordinates').validate(function(coordinates){
^
TypeError: Cannot call method 'validate' of undefined
谁能告诉我这为什么失败了?请注意,我只是验证path('location'),它的开头很好.
Can anyone advise why this is failing? Note is I just validate path('location'), its starts fine.
推荐答案
要在名为type
的嵌入式对象中定义字段,您需要使用显式对象符号来定义其类型,或者Mongoose认为其正在定义对象的类型.父对象代替:
To define a field in an embedded object named type
, you need to define its type using the explicit object notation or Mongoose thinks it's defining the type of the parent object instead:
var locationSchema = new Schema({
userid: { type: Number, required: true },
location: {
type: { type: [{
type: "String",
required: true,
enum: ['Point', 'LineString', 'Polygon'],
default: 'Point'
}]},
coordinates: { type: [Number], required:true }
},
tags: [{ type: String, index: true, required: true }],
create_date: { type: Date, default: Date.now }
});
这篇关于Node.js-猫鼬路径验证失败-TypeError:无法调用未定义的方法"validate"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文