提问者:小点点

如何修复未处理的promise拒绝警告,强制转换为objectid失败


我是mongoose的新手,我正在尝试在我的api中设置一个到'/featured'的获取路由,但是收到以下错误“(节点:8989)UnhandledPromiserEjectionWarning:CastError:将模型”blog“的路径”_id“处的值”featured'转换到ObjectId失败“

我很确定我只是在为我的博客设置路由器时做了一些错误的事情。我已经尝试使用。find({'feature':true}),try.find({'feature':true}),try.find()。where('feature',true),try.find()。where('feature')。equals(true),所有这些都导致相同的UnhandledPromiserEjectionWarning:CastError

这是我的博客模式

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BlogSchema = new Schema({
    title: { type: String, required: true },
    article: { type: String, required: true },
    published: { type: Date, required: true },
    featured: { type: Boolean, required: true },
    author: { type: Schema.Types.ObjectId, ref: 'User', required:true}
});

module.exports = mongoose.model('Blog', BlogSchema); 

下面是我在blogs.js中遇到麻烦的路由

router.get('/featured', (req, res) => 
{
    Blog
        .find({'featured': true})
        .then(blogs => 
        {
            if(blogs){
                res.status(200).json(blogs)
            }
            else console.log('blogs not found');
        })
        .catch(err => console.log(err));
}); 

以下是错误堆栈跟踪

(node:16486) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Server is listening on http://localhost:8080
(node:16486) UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "featured" at path "_id" for model "Blog"
    at new CastError (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/error/cast.js:29:11)
    at ObjectId.cast (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/schema/objectid.js:244:11)
    at ObjectId.SchemaType.applySetters (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/schematype.js:948:12)
    at ObjectId.SchemaType._castForQuery (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/schematype.js:1362:15)
    at ObjectId.SchemaType.castForQuery (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/schematype.js:1352:15)
    at ObjectId.SchemaType.castForQueryWrapper (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/schematype.js:1331:15)
    at cast (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/cast.js:307:32)
    at model.Query.Query.cast (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/query.js:4575:12)
    at model.Query.Query._castConditions (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/query.js:1783:10)
    at model.Query.<anonymous> (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/query.js:2038:8)
    at model.Query._wrappedThunk [as _findOne] (/home/taylour/projects/node200/node200-mongoose-blog-api/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
    at process.nextTick (/home/taylour/projects/node200/node200-mongoose-blog-api/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)
(node:16486) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

我希望“/featured”路由返回所有“featured”布尔值为true的博客,但实际上,无论对此路由的查询进行何种排列,我都会得到这个错误


共1个答案

匿名用户

我是mongoose的新手,我正在尝试在我的api中设置一个到'/featured'的获取路由,但是收到以下错误“(节点:8989)UnhandledPromiserEjectionWarning:CastError:将模型”blog“的路径”_id“处的值”featured'转换到ObjectId失败“

这意味着在mongo集合中有一个类似于{_id:“featured”}的文档。“featured”不是ObjectId,因此mongoose在看到该文档时会出错,因为它不知道如何处理该文档。