getById() returns 未定义的承诺

getById() returns undefined Promise

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

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) 调用 returns 一个承诺,所以你应该首先通过 then 解决承诺,然后用 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
  });