无法读取未定义的属性'catch'

问题描述 投票:1回答:2

我试图在Node中使用Argon2加密,但是当我尝试加密字符串时,我收到此错误:

Cannot read property 'catch' of undefined

我试过处理argon2.hash函数返回的promise中的错误,但它仍然无法正常工作。

到目前为止这是我的代码:

argon2.hash('password', {type: argon2.argon2id})
    .then(hash => {
        // do something with the hash
    }).catch(err => {
        // Handle the error
    });

有人可以帮我解决这个错误吗?

node.js promise
2个回答
1
投票

它抛出一个异常,它不会返回一个promise。因此,没有promise对象可以调用then(...).catch(...)方法。

要捕获它,您需要一个实际的try / catch块

来自argon2 github page,你应该这样做:

const argon2 = require('argon2');

try {
  const hash = await argon2.hash("password");
} catch (err) {
  //...
}

0
投票

请尝试以下方法:

argon2.hash('password', {type: argon2.argon2id})
  .then(hash => {
    // do something with the hash
}, err => {
    // Handle the error
});

then子句的第二个参数是onError处理程序。

© www.soinside.com 2019 - 2024. All rights reserved.