是否需要定义errorMessages
子文档来保存具有混合类型的数组? (另外,当我必须在该子文档数组中混合类型时,该怎么做)
仅使用数组会导致看不到任何错误消息,只有一个空数组可见。使用mongoose.Mixed
作为类型会导致显示字符串错误,但是错误对象被忽略了。
我对如何实现这一目标感到困惑
// 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();
答案 0 :(得分:0)
不需要使用[Object]
,因为它等效于Mixed SchemaType的数组[]
。
猫鼬不会(或至少在我的情况下)保存默认的节点错误,该错误由const nodeError = new Error("Error Message")
创建,但是如果错误被分解为新的对象const newObject = {message: nodeError.message, stack: nodeError.stack}
且newObject
被推送到errorMessages数组,它将按预期保存!