亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取錯誤 ValidatorError: Path `id` 是必需的。在 mongo 數據庫中

獲取錯誤 ValidatorError: Path `id` 是必需的。在 mongo 數據庫中

慕哥9229398 2021-10-14 17:07:15
保存document收藏時出現此錯誤ValidatorError: 路徑id是必需的。這是我的代碼 https://codesandbox.io/s/lively-tree-hd0foconst BlogPost = new Schema({  id: { type: String, required: true, unique: true },  empid: String,  date: Date});BlogPost.pre("save", function(next) {  var blog = this;  console.log();  var data = `${blog.empid}-${blog.date}`;  blog.id = crypto    .createHash("md5")    .update(data)    .digest("hex");  next();});當我嘗試保存數據時出現錯誤。一種pp.get("/saveData", async () => {  try {    var blog = new BlogPostModel({      empid: "test123",      date: "19-Jul-2019"    });    console.log("before save");    let saveBlog = await blog.save(); //when fail its goes to catch    console.log(saveBlog); //when success it print.    console.log("saveBlog save");  } catch (error) {    console.log(error);  }});
查看完整描述

2 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

當您保存博客文檔時,您不會傳遞 id 字段。

id 由 mongo 自動生成,您無需指定。從 BlogPost 架構中刪除 id。但是如果你想給你自己的id然后傳遞一個唯一的id。


const BlogPost = new Schema({

  empid: String,

  date: Date

});


查看完整回答
反對 回復 2021-10-14
  • 2 回答
  • 0 關注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號