Blubird 从不点击 .catch 语句
Blubird never hits the .catch statement
我正在使用 Node.js、Bluebird 和 Redis。给定:
redisClient.hmsetAsync([key, 'sn', sn, 'make', make])
.then(redisClient.setAsync(key + ":radio", radioArray))
.then(TMatic.send(res, 200))
.catch(function (e) {
console.log("Error reading file", e);
TMatic.send(res, 500);
});
当"radioArray"为空时,redis抛出异常:
Unhandled rejection Error: ERR wrong number of arguments for command
此代码始终发回 200 响应。为什么它没有命中 .catch() 方法并抛出 500?
回答!
经过大约半小时的黑客攻击,我想出了这个。谢谢macareno.marco!
redisClient.hmsetAsync([key, 'sn', sn, 'make', make])
.then(function saveRadios() {
if (radioArray.length) {
console.log("radio");
return redisClient.setAsync(key + ":radio", radioArray)
}
})
.then(function () {
TMatic.send(res, 200);
})
.catch(function (e) {
console.log("Error:", e);
TMatic.fail(e, res);
});
使用线路时:
.then(TMatic.send(res, 200))
正在评估参数,并等待返回一个函数,以便 bluebird 可以调用它。您应该改用函数,如下所示:
.then(function(res) {
TMatic.send(res, 200);
}
我正在使用 Node.js、Bluebird 和 Redis。给定:
redisClient.hmsetAsync([key, 'sn', sn, 'make', make])
.then(redisClient.setAsync(key + ":radio", radioArray))
.then(TMatic.send(res, 200))
.catch(function (e) {
console.log("Error reading file", e);
TMatic.send(res, 500);
});
当"radioArray"为空时,redis抛出异常:
Unhandled rejection Error: ERR wrong number of arguments for command
此代码始终发回 200 响应。为什么它没有命中 .catch() 方法并抛出 500?
回答! 经过大约半小时的黑客攻击,我想出了这个。谢谢macareno.marco!
redisClient.hmsetAsync([key, 'sn', sn, 'make', make])
.then(function saveRadios() {
if (radioArray.length) {
console.log("radio");
return redisClient.setAsync(key + ":radio", radioArray)
}
})
.then(function () {
TMatic.send(res, 200);
})
.catch(function (e) {
console.log("Error:", e);
TMatic.fail(e, res);
});
使用线路时:
.then(TMatic.send(res, 200))
正在评估参数,并等待返回一个函数,以便 bluebird 可以调用它。您应该改用函数,如下所示:
.then(function(res) {
TMatic.send(res, 200);
}