Mongoose验证错误发生在OpenShift上,但不是本地版本(Mongoose Validation Error occurs on OpenShift but not local versio

编程入门 行业动态 更新时间:2024-10-25 15:35:48
Mongoose验证错误发生在OpenShift上,但不是本地版本(Mongoose Validation Error occurs on OpenShift but not local version)

我正在使用Mongoose将我的Node.js服务器迁移到OpenShift,并且在我的本地WebStorm内置服务器上无法重现的实时服务器上发生错误。

我收到错误消息:

undefined: { properties: { message: "Cannot read property 'options' of undefined" type: "cast" }- message: "Cannot read property 'options' of undefined" name: "ValidatorError" kind: "cast" }

当我尝试将元素推入items数组并保存时,会发生以下情况:

var listSchema = new mongoose.Schema({ owner: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true}, name: {type: String, required: true}, items: [ { name:{ type: String, required:true }, quantity:Number, check: Boolean } ] });

有效的本地版本和OpenShift版本使用完全相同的代码。 添加新元素的代码是:

var listId = req.params["id"]; if (sessions.verifyToken(userId, token)) { var data = req.body; var query = List.findOne({ owner: userId, "_id": listId }); query.exec(function(err, list) { ... //handle error and null (omitted for brevity) ... list.items.push({ // error thrown here name: req.body["name"], quantity: req.body["quantity"], check: false }); list.save(function(err, list) { if (err) { var message = "Unable save appended list to persistent memory"; console.log(message, err); res.setHeader("content-type", "application/json"); res.send(JSON.stringify({success: false, message: message, error: err})); return; } res.setHeader("content-type", "application/json"); res.send(JSON.stringify(list)); }); });

我认为可能早期版本的模式添加了一个约束,所以我删除了lists集合,但问题并没有消失。

可能导致错误的OpenShift PaaS可能有什么不同?

[编辑]为了好玩,我从items删除了所有必填字段,现在错误信息是这样的:

"undefined": { "properties": { "message": "Cannot read property 'options' of undefined", "type": "cast" }, "message": "Cannot read property 'options' of undefined", "name": "ValidatorError", "kind": "cast" }, "owner": { "properties": { "type": "required", "message": "Path `{PATH}` is required.", "path": "owner" }, "message": "Path `owner` is required.", "name": "ValidatorError", "kind": "required", "path": "owner" }

这似乎表明模型在查找列表和再次保存列表之间的某处丢失了owner字段。 [/编辑]

I am migrating my Node.js server with Mongoose to OpenShift and an error occurs on the live server that I cannot reproduce on my local WebStorm built-in server.

I get the error message:

undefined: { properties: { message: "Cannot read property 'options' of undefined" type: "cast" }- message: "Cannot read property 'options' of undefined" name: "ValidatorError" kind: "cast" }

This occurs when I try to push an element into the items array and save, for the following schema:

var listSchema = new mongoose.Schema({ owner: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true}, name: {type: String, required: true}, items: [ { name:{ type: String, required:true }, quantity:Number, check: Boolean } ] });

The local version that works, and the OpenShift version use the exact same code. The code that adds the new element is:

var listId = req.params["id"]; if (sessions.verifyToken(userId, token)) { var data = req.body; var query = List.findOne({ owner: userId, "_id": listId }); query.exec(function(err, list) { ... //handle error and null (omitted for brevity) ... list.items.push({ // error thrown here name: req.body["name"], quantity: req.body["quantity"], check: false }); list.save(function(err, list) { if (err) { var message = "Unable save appended list to persistent memory"; console.log(message, err); res.setHeader("content-type", "application/json"); res.send(JSON.stringify({success: false, message: message, error: err})); return; } res.setHeader("content-type", "application/json"); res.send(JSON.stringify(list)); }); });

I thought that maybe an earlier version of the schema had added a constraint, so I dropped the lists collection, but the problem did not go away.

What might be different on the OpenShift PaaS that could be causing the error?

[Edit] Just for fun, I removed all required fields from items and now the error message is this:

"undefined": { "properties": { "message": "Cannot read property 'options' of undefined", "type": "cast" }, "message": "Cannot read property 'options' of undefined", "name": "ValidatorError", "kind": "cast" }, "owner": { "properties": { "type": "required", "message": "Path `{PATH}` is required.", "path": "owner" }, "message": "Path `owner` is required.", "name": "ValidatorError", "kind": "required", "path": "owner" }

This seems to suggest that the Model loses its owner field somewhere between finding the list and saving it again. [/Edit]

最满意答案

在OpenShift上,当您find或find一个对另一个实体具有必需引用的模型时,该字段将不会自动填充。因此,当调用save时,该字段将丢失。 更改

var query = List.findOne({ owner: userId, "_id": listId });

var query = List.findOne({ owner: userId, "_id": listId }).populate("owner");

出于某种原因,这在每个环境中都不起作用。 对于某些人来说,要么自动填充参考字段,要么在保存时假设它不变。 我不确定哪个。

On OpenShift, when you find or findOne a model that has a required reference to another entity, that field will not be automatically filled in. Thus, when save is called, the field will be missing. Change

var query = List.findOne({ owner: userId, "_id": listId });

to

var query = List.findOne({ owner: userId, "_id": listId }).populate("owner");

For some reason, this does not work the same in every environment. For some, either it does automatically populate the reference field, or it assumed it unchanged when saving. I'm not sure which.

更多推荐

本文发布于:2023-07-29 15:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   版本   发生在   OpenShift   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!