首页 > 解决方案 > 命中 GET 路由时出错,但数据显示

问题描述

当到达一条获取路线时,我得到了我期望的数据,但看起来我的路线被击中了两次:一次有数据,另一次没有提供信息。

如果我导航到一个 URL:https ://www.something.com/events/XYZ然后我得到所有数据并且页面被正确填充。除非我在浏览器中单击“X”,否则该页面似乎永远不会完成加载。

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

标签: node.jsexpressmongoose

解决方案


“双重打击”是由页面上的搜索栏引起的。如果没有输入信息,它将在没有信息的情况下到达路线并导致错误。为简单起见,我将搜索功能移至专用页面。感谢各位的帮助!


推荐阅读