gpt4 book ai didi

node.js - CastError:在模型“页面”的路径“_id”处,对于值“5be86bf8170c2c22f8bb93a6”的强制转换为ObjectId失败

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:04 25 4
gpt4 key购买 nike

我正在使用node / express / mongo / mongoose构建一个应用程序。我遇到了一个似乎无法弄清的错误,到目前为止,对Google进行谷歌搜索一直没有帮助。

我尝试创建一个编辑页面,但遇到错误。

Package.json

{
"name": "cmscart",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"connect-flash": "^0.1.1",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-messages": "^1.0.1",
"express-session": "^1.15.6",
"express-validator": "^5.3.0",
"mongoose": "^5.3.10"
}
}


edit_page.ejs

<%- include('../_layouts/adminheader')  %>

<h2 class="page-title">Edit a Page</h2>
<a href="/admin/pages" class="btn btn-primary">Back to all Pages</a>
<br>
<br>

<form method="post" action="/admin/pages/edit-page/<%= slug %> ">
<div class="form-group">
<label for="">Title</label>
<input type="text" name="title" value="<%= title %>" class="form-control" placeholder="Title">

</div>

<div class="form-group">
<label for="">Slug</label>
<input type="text" name="slug" value="<%= slug %>" class="form-control" placeholder="Slug">

</div>
<div class="form-group">
<label for="">Content</label>
<textarea name="content" class="form-control" placeholder="Content" rows="10" cols="30"><%= content %></textarea>

</div>

<input type="hidden" name="id" value="<%= id %> ">
<button class="btn btn-default">Submit</button>
</form>


<%- include('../_layouts/adminfooter') %>


admin_pages.js

/* GET edit page*/

/* here after edit page "/:slug" because we didn't have a fixed value of url*/
router.get('/edit-page/:slug', function (req, res) {

Page.findOne({ slug: req.params.slug }, function (err, page) {

if (err) return console.log(err);

res.render('admin/edit_page', {
title: page.title,
slug: page.slug,
content: page.content,
id: page._id
});
});


});




/* POST edit page*/



router.post('/edit-page/:slug', function (req, res) {
req.checkBody('title', 'Title must have a value').notEmpty();
req.checkBody('content', 'Content must have a value').notEmpty();

var title = req.body.title;
var slug = req.body.slug.replace(/\s+/g, '-').toLowerCase();
if (slug == "") slug = title.replace(/\s+/g, '-').toLowerCase();

var content = req.body.content;
var id = req.body.id;


var errors = req.validationErrors();
if (errors) {
res.render('admin/edit_page', {
errors: errors,
title: title,
slug: slug,
content: content,
id: id
});
} else {
Page.findOne({ slug: slug, _id: { '$ne': id } }, function (err, page) {

if (page) {
req.flash('danger', 'Page slug exists , choose another.');
res.render('admin/edit_page', {
title: title,
slug: slug,
content: content,
id:id
});
}
else {
Page.findByIdAndUpdate(id, function (err, page) {
if (err) {
return console.log(err);
}
page.title = title;
page.slug = slug;
page.content = content;


page.save(function (err) {
if (err)
return console.log(err);
req.flash('success', 'Page added!');
res.redirect('/admin/pages');

});
});
}
});
}


});


在此我遇到了这个错误

CastError:在模型“页面”的路径“ _id”处,将值“ 5be86bf8170c2c22f8bb93a6”转换为ObjectId失败
    在新的CastError(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ error \ cast.js:29:11)
    在ObjectId.cast(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schema \ objectid.js:156:13)
    在ObjectId.SchemaType.applySetters(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:763:12)
    在ObjectId.SchemaType._castForQuery(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1166:15)
    在ObjectId.SchemaType.castForQuery(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1156:15)
    在ObjectId.SchemaType.castForQueryWrapper(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1135:15)
    在投放时(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ cast.js:306:32)
    在model.Query.Query.cast(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:4024:12)
    在model.Query.Query._castConditions(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:1690:10)
    在model.Query.Query._findOne(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:1926:8)
    在process.nextTick(D:\ projects \ cmscart \ node_modules \ kareem \ index.js:369:33)
    在_combinedTickCallback(内部/进程/next_tick.js:132:7)
    在process._tickCallback(内部/进程/next_tick.js:181:9)
  消息:“在模型“页面”的路径“ _id”处,对值5be86bf8170c2c22f8bb93a6的对象ID的发布失败”,
  名称:“ CastError”,
  stringValue:'“ 5be86bf8170c2c22f8bb93a6”',
  种类:“ ObjectId”,
  值:“ 5be86bf8170c2c22f8bb93a6”,
  路径:“ _ id”,
  原因:未定义,
  模型:
   {[功能:型号]
     钩子:Kareem {_pres:[Object],_posts:[Object]},
     基础:
      猫鼬{
        连接:[数组],
        型号:[对象],
        modelSchemas:[Object],
        选项:[对象],
        _pluralize:[功能:复数],
        插件:[Array]},
     modelName:“页面”,
     型号:[功能:型号],
     D b:
      NativeConnection {
        基本:[对象],
        集合:[对象],
        型号:[对象],
        配置:[对象],
        副本:false,
        选项:null,
        otherDbs:[],
        relatedDbs:{},
        状态:[对象],
        _readyState:1
        _closeCalled:否,
        _hasOpened:是的,
        '$ internalEmitter':[Object],
        _listening:错误,
        _connectionOptions:[Object],
        名称:“ cmscart”,
        主机:“ localhost”,
        端口:27017,
        用户:null,
        通过:null,
        客户:[对象],
        '$ initialConnection':[Object],
        _events:[对象],
        _eventsCount:1
        db:[Object]},
     鉴别符:未定义,
     '$ appliedMethods':是的,
     '$ appliedHooks':是的,
     模式:
      架构{
        obj:[Object],
        路径:[对象],
        别名:{},
        子路径:{},
        虚拟对象:[对象],
        singleNestedPaths:{},
        嵌套:{},
        继承:{},
        callQueue:[],
        _indexes:[],
        方法: {},
        methodOptions:{},
        静态值:{},
        树:[对象],
        查询:{},
        childSchemas:[],
        插件:[Array],
        '$ id':1,
        s:[Object],
        _userProvidedOptions:{},
        选项:[对象],
        '$ globalPluginsApplied':true},
     采集:
      NativeCollection {
        集合:[对象],
        选择:[对象],
        名称:“ pages”,
        collectionName:“页面”,
        conn:[对象],
        队列:[],
        缓冲区:false,
        发射器:[Object]},
     查询:{[功能]基础:[对象]},
     '$ __ insertMany':[函数],
     '$ init':承诺{[Circular]},
     '$ caught':true}}
{CastError:在模型“页面”的路径“ _id”处,对于值“ 5be86bf8170c2c22f8bb93a6”的铸造为ObjectId失败
    在新的CastError(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ error \ cast.js:29:11)
    在ObjectId.cast(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schema \ objectid.js:156:13)
    在ObjectId.SchemaType.applySetters(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:763:12)
    在ObjectId.SchemaType._castForQuery(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1166:15)
    在ObjectId.SchemaType.castForQuery(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1156:15)
    在ObjectId.SchemaType.castForQueryWrapper(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1135:15)
    在投放时(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ cast.js:306:32)
    在model.Query.Query.cast(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:4024:12)
    在model.Query.Query._castConditions(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:1690:10)
    在model.Query.Query._findOne(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:1926:8)
    在process.nextTick(D:\ projects \ cmscart \ node_modules \ kareem \ index.js:369:33)
    在_combinedTickCallback(内部/进程/next_tick.js:132:7)
    在process._tickCallback(内部/进程/next_tick.js:181:9)
  消息:“在模型“页面”的路径“ _id”处,对值5be86bf8170c2c22f8bb93a6的对象ID的发布失败”,
  名称:“ CastError”,
  stringValue:'“ 5be86bf8170c2c22f8bb93a6”',
  种类:“ ObjectId”,
  值:“ 5be86bf8170c2c22f8bb93a6”,
  路径:“ _ id”,
  原因:未定义,
  模型:
   {[功能:型号]
     钩子:Kareem {_pres:[Object],_posts:[Object]},
     基础:
      猫鼬{
        连接:[数组],
        型号:[对象],
        modelSchemas:[Object],
        选项:[对象],
        _pluralize:[功能:复数],
        插件:[Array]},
     modelName:“页面”,
     型号:[功能:型号],
     D b:
      NativeConnection {
        基本:[对象],
        集合:[对象],
        型号:[对象],
        配置:[对象],
        副本:false,
        选项:null,
        otherDbs:[],
        relatedDbs:{},
        状态:[对象],
        _readyState:1
        _closeCalled:否,
        _hasOpened:是的,
        '$ internalEmitter':[Object],
        _listening:错误,
        _connectionOptions:[Object],
        名称:“ cmscart”,
        主机:“ localhost”,
        端口:27017,
        用户:null,
        通过:null,
        客户:[对象],
        '$ initialConnection':[Object],
        _events:[对象],
        _eventsCount:1
        db:[Object]},
     鉴别符:未定义,
     '$ appliedMethods':是的,
     '$ appliedHooks':是的,
     模式:
      架构{
        obj:[Object],
        路径:[对象],
        别名:{},
        子路径:{},
        虚拟对象:[对象],
        singleNestedPaths:{},
        嵌套:{},
        继承:{},
        callQueue:[],
        _indexes:[],
        方法: {},
        methodOptions:{},
        静态值:{},
        树:[对象],
        查询:{},
        childSchemas:[],
        插件:[Array],
        '$ id':1,
        s:[Object],
        _userProvidedOptions:{},
        选项:[对象],
        '$ globalPluginsApplied':true,
        _requiredpaths:[Array]},
     采集:
      NativeCollection {
        集合:[对象],
        选择:[对象],
        名称:“ pages”,
        collectionName:“页面”,
        conn:[对象],
        队列:[],
        缓冲区:false,
        发射器:[Object]},
     查询:{[功能]基础:[对象]},
     '$ __ insertMany':[函数],
     '$ init':承诺{[Circular]},
     '$ caught':true}}
{CastError:在模型“页面”的路径“ _id”处,对于值“ 5be86bf8170c2c22f8bb93a6”的铸造为ObjectId失败
    在新的CastError(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ error \ cast.js:29:11)
    在ObjectId.cast(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schema \ objectid.js:156:13)
    在ObjectId.SchemaType.applySetters(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:763:12)
    在ObjectId.SchemaType._castForQuery(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1166:15)
    在ObjectId.SchemaType.castForQuery(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1156:15)
    在ObjectId.SchemaType.castForQueryWrapper(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ schematype.js:1135:15)
    在投放时(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ cast.js:306:32)
    在model.Query.Query.cast(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:4024:12)
    在model.Query.Query._castConditions(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:1690:10)
    在model.Query.Query._findOne(D:\ projects \ cmscart \ node_modules \ mongoose \ lib \ query.js:1926:8)
    在process.nextTick(D:\ projects \ cmscart \ node_modules \ kareem \ index.js:369:33)
    在_combinedTickCallback(内部/进程/next_tick.js:132:7)
    在process._tickCallback(内部/进程/next_tick.js:181:9)
  消息:“在模型“页面”的路径“ _id”处,对值5be86bf8170c2c22f8bb93a6的对象ID的发布失败”,
  名称:“ CastError”,
  stringValue:'“ 5be86bf8170c2c22f8bb93a6”',
  种类:“ ObjectId”,
  值:“ 5be86bf8170c2c22f8bb93a6”,
  路径:“ _ id”,
  原因:未定义,
  模型:
   {[功能:型号]
     钩子:Kareem {_pres:[Object],_posts:[Object]},
     基础:
      猫鼬{
        连接:[数组],
        型号:[对象],
        modelSchemas:[Object],
        选项:[对象],
        _pluralize:[功能:复数],
        插件:[Array]},
     modelName:“页面”,
     型号:[功能:型号],
     D b:
      NativeConnection {
        基本:[对象],
        集合:[对象],
        型号:[对象],
        配置:[对象],
        副本:false,
        选项:null,
        otherDbs:[],
        relatedDbs:{},
        状态:[对象],
        _readyState:1
        _closeCalled:否,
        _hasOpened:是的,
        '$ internalEmitter':[Object],
        _listening:错误,
        _connectionOptions:[Object],
        名称:“ cmscart”,
        主机:“ localhost”,
        端口:27017,
        用户:null,
        通过:null,
        客户:[对象],
        '$ initialConnection':[Object],
        _events:[对象],
        _eventsCount:1
        db:[Object]},
     鉴别符:未定义,
     '$ appliedMethods':是的,
     '$ appliedHooks':是的,
     模式:
      架构{
        obj:[Object],
        路径:[对象],
        别名:{},
        子路径:{},
        虚拟对象:[对象],
        singleNestedPaths:{},
        嵌套:{},
        继承:{},
        callQueue:[],
        _indexes:[],
        方法: {},
        methodOptions:{},
        静态值:{},
        树:[对象],
        查询:{},
        childSchemas:[],
        插件:[Array],
        '$ id':1,
        s:[Object],
        _userProvidedOptions:{},
        选项:[对象],
        '$ globalPluginsApplied':true,
        _requiredpaths:[Array]},
     采集:
      NativeCollection {
        集合:[对象],
        选择:[对象],
        名称:“ pages”,
        collectionName:“页面”,
        conn:[对象],
        队列:[],
        缓冲区:false,
        发射器:[Object]},
     查询:{[功能]基础:[对象]},
     '$ __ insertMany':[函数],
     '$ init':承诺{[Circular]},
     '$ caught':true}}

最佳答案

"5be86bf8170c2c22f8bb93a6 "的末尾有一个空格字符。你先尝试修剪吗?

在文件D:\projects\cmscart\node_modules\kareem\index.js第369行中,使用id.trim()代替id以及您使用的任何位置,或者您可以首先在开头更正id

关于node.js - CastError:在模型“页面”的路径“_id”处,对于值“5be86bf8170c2c22f8bb93a6”的强制转换为ObjectId失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53304590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com