我試圖在任何地方找到這個問題的答案,但看起來我運氣不好。貓鼬{strict:throw}不會拋出錯誤
我有一個非常簡單的貓鼬模型
var userObject = {
profile: {
username: {
type: String,
required: true,
lowercase: true
},
firstname: {
type: String,
required: true
},
lastname: {
type: String,
required: true
},
img: {
type: String,
required: true,
match: /^(https?:\/\/)/i
},
email: {
type: String,
match: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-][email protected][a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
required: true
},
singupdate: {
type: Date,
default: Date.now
}
}
};
而且,當我創建架構我選擇的選項時,我沒有在模型中添加屬性拋出一個錯誤。
new mongoose.Schema(userObject, { strict: "throw" });
這就是我試圖捕捉錯誤的方法。當我添加有效的屬性時,進程運行,並且重新創建所創建的文檔,但是當我添加無效的屬性時,該進程永遠不會退出,並且日誌永遠不會顯示在控制檯上。
try {
User.create(users, function(err, docs) {
console.log("err: " + err);
console.log("docs: " + docs);
});
} catch (e) {
console.log(e.message);
}
我做錯了什麼?
有沒有辦法知道哪些字段沒有保存? – Leia
在您的模型中爲您的字段添加'required:true'確保它們將被保存,否則它們不是必需的,如果它們不是輸入的一部分,則不會被報告 –