猫鼬不保存混合类型的数组

时间:2020-02-11 11:15:28

标签: javascript node.js mongoose

是否需要定义errorMessages子文档来保存具有混合类型的数组? (另外,当我必须在该子文档数组中混合类型时,该怎么做)

仅使用数组会导致看不到任何错误消息,只有一个空数组可见。使用mongoose.Mixed作为类型会导致显示字符串错误,但是错误对象被忽略了。

我对如何实现这一目标感到困惑

我的模式

Schema

保存前的我的文档

Schema before save

保存后我的文档

Schema after save

代码


// document is APIATicket
// error is either a string or an Error Object that has been created with new Error()
document.errorMessages.push(error);
document.save();

// other "normal" objects (created with object literals) result in the same behavior
const myObject = {msg: "Hello world"};
document.payload = myObject;
document.save();

1 个答案:

答案 0 :(得分:0)

  1. 不需要使用[Object],因为它等效于Mixed SchemaType的数组[]

  2. 猫鼬不会(或至少在我的情况下)保存默认的节点错误,该错误由const nodeError = new Error("Error Message")创建,但是如果错误被分解为新的对象const newObject = {message: nodeError.message, stack: nodeError.stack}newObject被推送到errorMessages数组,它将按预期保存!