【问题标题】:Is the following schema model declared correctly?以下模式模型是否正确声明?
【发布时间】:2016-11-21 00:29:35
【问题描述】:

我是 Mongoose 和 nodejs 的新手。尝试做一个简单的项目。我正在尝试创建一个具有以下字段的架构。我正在尝试在前端对其进行字符串化,并在后端对其进行 JSON.parse。 我收到一个错误“转换为数组失败的值”。

var studentSchema = new Schema({
     
    profileNum              : {type:String},
    eMail                   : {type:String},
	preferredLocation	    : [],
	gender 	                : {type:String,default:''},
	education               : [{institute:String,batch:Number,course:String}],
	profession              : [],
	designation             : {type:String,default:''},
	organization            : {type:String,default:''}	

});

【问题讨论】:

  • 你从哪里得到这个错误?
  • @DevDig 在前端,我正在执行这个$scope.education = JSON.stringify($scope.education);,然后发送数据。在后端执行此操作var educationDetail = req.body.education; educationDetail = JSON.parse(educationDetail); result.education = educationDetail;
  • $scope.education 长什么样子?
  • @str ["something",123546,"gh"] 具体的错误是这个err ValidationError: CastError: Cast to Array failed for value "[ 'something', 123546, 'gh' ]" at path "education"
  • 你在后端的 req.body 中发送什么?

标签: javascript node.js mongoose mongoose-schema


【解决方案1】:

对于您的education 路径,您可以使用[Schema.Types.Mixed] 或为教育子文档创建一个新架构并使用[EducationSchema],如果您需要类型或其他中间件/验证。

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多