【问题标题】:UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:UnhandledPromiseRejectionWarning:AssertionError [ERR_ASSERTION]:表达式评估为虚假值:
【发布时间】:2018-10-25 07:33:27
【问题描述】:

我在使用节点版本 10 和 MongoDB 数据库运行 Mocha 测试时遇到了这个巨大的错误:

(node:27884) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]:表达式评估为假值:

断言( user.blogPosts[0].cmets[0].content === '随着程序变得越来越大,它们也变得越来越复杂')

at User.findOne.populate.then.user (/Users/danale/Projects/users/test/association_test.js:50:9)
at process._tickCallback (internal/process/next_tick.js:68:7) (node:27884) UnhandledPromiseRejectionWarning: Unhandled promise

拒绝。此错误源于在异步内部抛出 没有 catch 块的函数,或者通过拒绝一个承诺 不使用 .catch() 处理。 (拒绝 ID:1)(节点:27884)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。

对于之前工作的 Mocha 测试:

it('saves a full relation graph', done => {
    User.findOne({ name: 'Joe' })
      .populate({
        path: 'blogPosts',
        populate: {
          path: 'comments',
          model: 'comment',
          populate: {
            path: 'user',
            model: 'user'
          }
        }
      })
      .then(user => {
        assert(user.name === 'Joe');
        assert(user.blogPosts[0].title === 'Eloquent JavaScript');
        assert(
          user.blogPosts[0].comments[0].content ===
            'As programs get bigger, they also become more complex'
        );
        assert(user.blogPosts[0].comments[0].user.name === 'Joe');
        done();
      });
  });

所以我知道问题出在这里:

assert(
   user.blogPosts[0].comments[0].content ===
     'As programs get bigger, they also become more complex'
);

但这到底有什么问题呢?

【问题讨论】:

  • 为什么不添加一个带有控制台和/或调试点的 catch 块来找出问题所在?

标签: javascript node.js


【解决方案1】:

您需要catch 块才能找到答案。 populate() 返回一个 promise,但是当 promise 失败时,它们需要被捕获和处理。

有关更多信息,请参见 the docs,但可以将其视为您可能已经熟悉的 try { } catch { } 块。

这里您的代码附加了 catch 以及发生的错误的日志。

it('saves a full relation graph', done => {
User.findOne({ name: 'Joe' })
  .populate({
    path: 'blogPosts',
    populate: {
      path: 'comments',
      model: 'comment',
      populate: {
        path: 'user',
        model: 'user'
      }
    }
  })
  .then(user => {
    assert(user.name === 'Joe');
    assert(user.blogPosts[0].title === 'Eloquent JavaScript');
    assert(
      user.blogPosts[0].comments[0].content ===
        'As programs get bigger, they also become more complex'
    );
    assert(user.blogPosts[0].comments[0].user.name === 'Joe');
    done();
  })
  .catch((err) => {
    console.error("Handling promise rejection", err);
  });
});

【讨论】:

  • XaxD,我试过了,但没有用。所以我再次查看了我的代码,我注意到我的断言失败了,因为我引用了错误的content。我正在测试这个blogPost = new BlogPost({ title: 'Eloquent JavaScript', content: 'As programs get bigger, they also become more complex' }); 而不是这个:comment = new Comment({ content: 'Love this post' });
【解决方案2】:

错误消息适用于我正在测试的content,因为我在it() 块中测试了错误的content。我正在我的beforeEach() 中测试content

beforeEach(done => {
    joe = new User({ name: 'Joe' });
    blogPost = new BlogPost({
      title: 'Eloquent JavaScript',
      content: 'As programs get bigger, they also become more complex'
    });

当我真的想在这里测试comment 对象中的content 时: comment = new Comment({ content: 'Love this post' });

一旦我将其更正为上述适当的content,所有测试都通过了。

【讨论】:

    猜你喜欢
    • 2019-09-16
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多