gpt4 book ai didi

javascript - Mongoose 中未处理的PromiseRejection警告

转载 作者:行者123 更新时间:2023-12-03 01:50:39 24 4
gpt4 key购买 nike

我有这样的类别架构:

var category_article_Schema = new Schema({
"article_id": { type: String, required: false, unique: false },
"category": String,
"article": { type: mongoose.Schema.Types.ObjectId, ref: 'article'},
});

var category_article_Schema = mongoose.model('category_article_Schema', category_article_Schema);
module.exports = category_article_Schema;

文章架构:

var articleSchema = new Schema({
"title": { type: String, required: true, unique: false },
"details": String,
"username": { type: String, required: true, unique: false },
"postImageUrl": String,
"url": String,
"categories": [String],
"created_at": { type: Date, default: Date.now }
});

var article = mongoose.model('article', articleSchema);
module.exports = article;

当我尝试使用填充方法获取基于类别的文章时,出现异常:UnhandledPromiseRejectionWarning: Unhandled promise rejection

function getPostByCategory(req, res, next) {
category_article_model.find({category: req.params.name})
.populate('article')
.exec()
.then(function(err, articlesByCategory) {
if(err) throw err;
console.log(articlesByCategory);
})
}

首先,错误的原因可能是什么?为什么?我尝试寻找答案,但每种情况下的问题都不同。

最佳答案

重构

function getPostByCategory(req, res, next) {
category_article_model.find({category: req.params.name})
.populate('article')
.exec()
.then(function(err, articlesByCategory) {
if(err) throw err;
console.log(articlesByCategory);
})
}

到此

function getPostByCategory(req, res, next) {
category_article_model.find({category: req.params.name})
.populate('article')
.exec()
.then(function(articlesByCategory) {
console.log(articlesByCategory);
})
.catch(function (err){
throw err; // or handle it
})
}

关于javascript - Mongoose 中未处理的PromiseRejection警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50429042/

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