我不明白我为什么会有这个问题:
CastError: Cast to ObjectId failed for value "5fa41e7f4ee57a30687e80e9 " at path "_id" for model "page"
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters,在此代码之后
router.post('/edit-page/:slog', (req, res) => {
req.checkBody('title', 'Le champs titre doit être remplis').notEmpty()
req.checkBody('content', 'Le champs content doit être remplis').notEmpty();
let title = req.body.title
let slog = req.body.slog.replace(/\x+/g, '-').toLowerCase();
if (slog == "") slog = title.replace(/\x+/g, '-').toLowerCase();
let content = req.body.content
let id = req.body.id
const errors = req.validationErrors();
if (errors) {
res.render('admin/edit_page', {
errors: errors,
title: title,
slog: slog,
content: content,
id: id
})
} else {
Page.findOne({ slog: slog, _id: { "$ne": id } }, (err, page) => {
if (page) {
req.flash('danger', 'Cette page existe, choisis une autre!');
res.render('admin/edit_page', {
title: title,
slog: slog,
content: content,
id: id
});
} else {
Page.findById(id, function (err, page) {
console.log(id)
if (err)
console.log(err)
page.title = title;
page.slog = slog;
page.content = content;
page.save(function (err) {
if (err) {
console.log(err)
} else {
req.flash('success', 'page sauvegardée')
res.redirect('/admin/pages')
}
})
})
}
})
}
})发布于 2020-11-12 12:10:24
字符串有一个最小的错误。在最后有一个空间。
字符串5fa41e7f4ee57a30687e80e9应该是5fa41e7f4ee57a30687e80e9
使用猫鼬,我已经检查了这个,并工作:
var id = mongoose.Types.ObjectId("5fa41e7f4ee57a30687e80e9");此外,如果您想检查这个question,我已经解释了它是如何工作的。
https://stackoverflow.com/questions/64803619
复制相似问题