Node.js 点击GET route时出错,但显示数据
当点击get路线时,我得到了我期望的数据,但看起来我的路线被点击了两次:一次是数据,另一次是没有提供信息 如果我导航到一个URL:那么我将获得所有数据,并且页面被正确填充。除非我在浏览器中单击“X”,否则页面似乎永远无法完成加载Node.js 点击GET route时出错,但显示数据,node.js,express,mongoose,Node.js,Express,Mongoose,当点击get路线时,我得到了我期望的数据,但看起来我的路线被点击了两次:一次是数据,另一次是没有提供信息 如果我导航到一个URL:那么我将获得所有数据,并且页面被正确填充。除非我在浏览器中单击“X”,否则页面似乎永远无法完成加载 app.get("/events/:id", function(req, res){ Event.findById(req.params.id, function (err, foundEvent){ if(err){
app.get("/events/:id", function(req, res){
Event.findById(req.params.id, function (err, foundEvent){
if(err){
console.log("beginning error");
console.log(err);
console.log("found this event: " + foundEvent);
console.log("ending error");
}else {
console.log("now entering normal loop");
console.log(foundEvent);
res.render("showevent", {event: foundEvent});
}
})
});
当我键入URL(复制/粘贴)时,我会得到以下内容(冗长,抱歉,不确定哪些部分可能相关)console.log:
now entering normal loop
{ _id: 5cf30944e75f2679f77287a2,
name: 'Memorial Day 2019',
date: 2019-06-01T23:24:52.063Z,
story: 'Memorial Day parade and Ceremony',
posts: [ { link: [Array], image: [], _id: 5cf30944e75f2679f77287a3 } ],
__v: 0 }
beginning error
{ CastError: Cast to ObjectId failed for value "main.js" at path "_id" for model "Event"
at new CastError (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/error/cast.js:29:11)
at ObjectId.cast (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/schema/objectid.js:242:11)
at ObjectId.SchemaType.applySetters (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/schematype.js:892:12)
at ObjectId.SchemaType._castForQuery (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/schematype.js:1304:15)
at ObjectId.SchemaType.castForQuery (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/schematype.js:1294:15)
at ObjectId.SchemaType.castForQueryWrapper (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/schematype.js:1273:15)
at cast (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/cast.js:307:32)
at model.Query.Query.cast (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/query.js:4529:12)
at model.Query.Query._castConditions (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/query.js:1762:10)
at model.Query.<anonymous> (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/query.js:2015:8)
at model.Query._wrappedThunk [as _findOne] (/home/scott/cchistory/cchistory/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
at process.nextTick (/home/scott/cchistory/cchistory/node_modules/kareem/index.js:369:33)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
message: 'Cast to ObjectId failed for value "main.js" at path "_id" for model "Event"',
name: 'CastError',
stringValue: '"main.js"',
kind: 'ObjectId',
value: 'main.js',
path: '_id',
reason: undefined,
model:
{ [Function: model]
hooks: Kareem { _pres: [Object], _posts: [Object] },
base:
Mongoose {
connections: [Array],
models: [Object],
modelSchemas: [Object],
options: [Object],
_pluralize: [Function: pluralize],
Schema: [Object],
model: [Function],
plugins: [Array] },
modelName: 'Event',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
plugins: [],
'$internalEmitter': [Object],
_listening: false,
_connectionOptions: [Object],
name: 'historydb',
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
client: [Object],
'$initialConnection': [Object],
db: [Object] },
discriminators: undefined,
events:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined },
'$appliedMethods': true,
'$appliedHooks': true,
_middleware: Kareem { _pres: [Object], _posts: [Object] },
'$__insertMany': [Function],
schema:
Schema {
obj: [Object],
paths: [Object],
aliases: {},
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: [Object],
query: {},
childSchemas: [Array],
plugins: [Array],
'$id': 6,
s: [Object],
_userProvidedOptions: {},
options: [Object],
'$globalPluginsApplied': true,
_requiredpaths: [] },
collection:
NativeCollection {
collection: [Object],
Promise: [Function: Promise],
opts: [Object],
name: 'events',
collectionName: 'events',
conn: [Object],
queue: [],
buffer: false,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$init': Promise { [Circular] },
'$caught': true,
[Symbol(mongoose#Model)]: true } }
found this event: undefined
ending error
现在进入正常循环
{u id:5cf30944e75f2679f77287a2,
名称:“2019阵亡将士纪念日”,
日期:2019-06-01T23:24:52.063Z,
故事:“阵亡将士纪念日游行和仪式”,
帖子:[{link:[Array],图片:[],_id:5cf30944e75f2679f77287a3}],
__v:0}
起始误差
{CastError:对于模型“事件”的路径“\u id”处的值“main.js”,转换为ObjectId失败
在新的CastError(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/error/cast.js:29:11)
在ObjectId.cast(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/schema/ObjectId.js:242:11)
位于ObjectId.SchemaType.applySetters(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/SchemaType.js:892:12)
位于ObjectId.SchemaType.\u castForQuery(/home/scott/cchistory/cchistory/node\u modules/mongoose/lib/SchemaType.js:1304:15)
位于ObjectId.SchemaType.castForQuery(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/SchemaType.js:1294:15)
位于ObjectId.SchemaType.castforqueryrapper(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/SchemaType.js:1273:15)
at cast(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/cast.js:307:32)
在model.Query.Query.cast(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/Query.js:4529:12)
在model.Query.Query.\u castConditions(/home/scott/cchistory/cchistory/node\u modules/mongoose/lib/Query.js:1762:10)
在model.Query中。(/home/scott/cchistory/cchistory/node_modules/mongoose/lib/Query.js:2015:8)
在model.Query._wrappedThunk[as _findOne](/home/scott/cchistory/cchistory/node_modules/mongoose/lib/helpers/Query/wrapThunk.js:16:8)
在process.nextTick(/home/scott/cchistory/cchistory/node_modules/kareem/index.js:369:33)
at _combinedTickCallback(内部/流程/下一步_tick.js:131:7)
在进程中。_tick回调(内部/process/next_tick.js:180:9)
消息:“对模型“事件”的路径“\u id”处的值“main.js”转换为ObjectId失败,
名称:“卡斯特罗”,
stringValue:“'main.js””,
种类:“ObjectId”,
值:“main.js”,
路径:“\u id”,
原因:未定义,
型号:
{[功能:模型]
hooks:Kareem{{u pres:[Object],{u posts:[Object]},
基数:
猫鼬{
连接:[数组],
模型:[对象],
modelSchemas:[对象],
选项:[对象],
_多元化:[功能:多元化],
架构:[对象],
型号:[功能],
插件:[Array]},
modelName:'事件',
型号:[功能:型号],
db:
国家联系{
base:[对象],
集合:[对象],
模型:[对象],
配置:[对象],
复制品:假,
选项:null,
其他数据库:[],
关系数据库:{},
国家:[反对],
_预备状态:1,
_这是错误的,
_是的,
插件:[],
“$internalEmitter”:[Object],
_听:错,
_connectionOptions:[对象],
名称:“historydb”,
主机:“localhost”,
港口:27017,
用户:未定义,
通过:未定义,
客户端:[对象],
“$initialConnection”:[Object],
db:[对象]},
鉴别器:未定义,
活动:
事件发射器{
域:空,
_事件:{},
_事件提示:0,
_maxListeners:未定义},
“$appliedMethods”:正确,
“$appliedHooks”:正确,
_中间件:Kareem{{u pres:[Object],{u posts:[Object]},
“$\uu insertMany”:[函数],
模式:
模式{
obj:[对象],
路径:[对象],
别名:{},
子路径:{},
虚拟对象:[对象],
SingleNestedPath:{},
嵌套:{},
继承:{},
呼叫队列:[],
_索引:[],
方法:{},
方法选项:{},
静态:{},
树:[对象],
查询:{},
childSchemas:[数组],
插件:[数组],
“$id”:6,
s:[对象],
_用户提供的选项:{},
选项:[对象],
“$globalPlugins应用”:true,
_所需路径:[]},
收藏:
本地采集{
集合:[对象],
承诺:[功能:承诺],
选项:[对象],
名称:'事件',
collectionName:'事件',
conn:[对象],
队列:[],
缓冲区:false,
发射器:[对象]},
查询:{[Function]base:[Object]},
“$init”:承诺{[Circular]},
“$catch”:是的,
[符号(猫鼬#模型)]:true}
发现此事件:未定义
结束错误
这一“双重打击”是由页面上的搜索栏造成的。输入not info后,它将击中没有信息的路线并导致错误。
为了简单起见,我将搜索功能移动到专用页面。
谢谢大家的帮助 你能分享你的
事件
模式吗?我找到了原因:有一个搜索栏(没有输入)总是点击同一条路线。我已经拉了搜索栏功能,一切正常。事情总是那么简单!