gpt4 book ai didi

node.js - getById() 返回未定义的 Promise

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:50 24 4
gpt4 key购买 nike

我的 Sequelize 模型中有以下类方法:

getById(id) {
return new Promise((resolve, reject) => {
var Conference = sequelize.models.conference;
Conference.findById(id).then(function(conference) {
if (_.isObject(conference)) {
resolve(conference);
} else {
throw new ResourceNotFound(conference.name, {id: id});
}
}).catch(function(err) {
reject(err);
});
});
}

现在我想用 chai 测试我的方法。但现在当我执行 Conference.getById(confereceId) 时,我得到以下信息:

Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }

这是正确的吗?我如何用 chai 断言它的结果?

最佳答案

您的 Conference.getById(confereceId) 调用返回一个 Promise,因此您应该首先通过 then 解析 Promise,并使用 chai 断言其结果,如下所示:

const assert = require('chai').assert;

Conference
.getById(confereceId)
.then(conf => {
assert.isObject(conf);
// ...other assertions
}, err => {
assert.isOk(false, 'conference not found!');
// no conference found, fail assertion when it should be there
});

关于node.js - getById() 返回未定义的 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607902/

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