保存document收藏时出现此错误
ValidatorError: 路径id是必需的。
这是我的代码 https://codesandbox.io/s/lively-tree-hd0fo
const 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);
}
});
慕后森
相关分类