首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传入的mongoose参数必须是一个12个字节的字符串或24个十六进制字符的字符串。

传入的mongoose参数必须是一个12个字节的字符串或24个十六进制字符的字符串。
EN

Stack Overflow用户
提问于 2020-11-12 12:06:11
回答 1查看 409关注 0票数 1

我不明白我为什么会有这个问题:

代码语言:javascript
复制
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,

在此代码之后

代码语言:javascript
复制
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')
                        }
                    })

                })

            }

        })
    }
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-12 12:10:24

字符串有一个最小的错误。在最后有一个空间。

字符串5fa41e7f4ee57a30687e80e9应该是5fa41e7f4ee57a30687e80e9

使用猫鼬,我已经检查了这个,并工作:

代码语言:javascript
复制
var id = mongoose.Types.ObjectId("5fa41e7f4ee57a30687e80e9");

此外,如果您想检查这个question,我已经解释了它是如何工作的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64803619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档