如何修复未处理的承诺拒绝警告,强制转换为objectid

时间:2019-06-16 18:16:20

标签: javascript mongodb mongoose

我是猫鼬的新手,我试图在我的api中设置指向'/ featured'的获取路径,但遇到以下错误'(node:8989)UnhandledPromiseRejectionWarning:CastError:Cast to ObjectId for value failed“型号“博客”的“ _id”路径中的“精选””

我相当确定在为博客设置路由器时,我只是在做错什么。我试过使用.find({'featured':true}),试过.find({featured:true}),试过.find()。where('featured',true),试过.find()。where ('featured')。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 个答案:

答案 0 :(得分:0)

  

我是猫鼬的新手,我试图在我的api中设置指向'/ featured'的获取路线,但遇到以下错误'(node:8989)UnhandledPromiseRejectionWarning:CastError:Cast >值“精选”在模型“博客”的路径“ _ id”上”

这表示您的mongo集合中有一个文档 ,看起来像{_id: "featured"}"featured"不是一个ObjectId,所以猫鼬在看到该文档时会出错,因为它不知道如何处理它。