gpt4 book ai didi

node.js - 在 node.js 中使用 express undefined variable 不会引发错误

转载 作者:IT老高 更新时间:2023-10-28 23:12:54 26 4
gpt4 key购买 nike

我正在使用 express 运行 node.js。我写了一个带有方法的 Node 模块,所以当你去 http://bla.com/module_name/method_name它将运行该方法。

该方法遵循典型的风格

exports.method_name(req, res, next);

我的主应用程序是这样的:

app.all("*", resSetup, controller, render);

Controller 是根据路径调用方法的东西。

似乎如果方法中有 undefined variable 错误,express会卡在那里而不抛出任何错误。控制台日志中也不会出现任何内容。我可以在错误发生之前和之后放置一条控制台消息,并且之前会出现在日志中,而之后不会。

我可以将它包装在 try/catch 中并得到这个:

[ReferenceError: blabla is not defined]

但没有行号或任何东西。

我的猜测是 express 以某种方式阻止了错误的出现。当我将错误放在直接位于路由中的名为“ Controller ”的函数中时,它会正确显示该错误。

这可能无关紧要,但这是我正在处理的代码:

https://github.com/RobKohr/quick-site/blob/master/index.js

第 189 行是方法调用发生的地方。

最佳答案

根据上面 Ruairi 的评论,我在使用 'q' ( https://github.com/kriskowal/q ) 和使用 express 的 promise 时遇到了同样的问题 - Node 会挂起并且不会产生错误。

通过在 promise “回调”链的末尾添加一个 catch,我能够看到错误并将其打印到控制台等。

代码最终看起来像:

export function index(req, res) {

//Create the 'promise'
var request = req.body;
var queryJobID = req.query.jobId;
console.log('queryJobID: ' + queryJobID);
var jobStatusPromsie = jobManager.job.getStatus(queryJobID);

Q.all([jobStatusPromsie])
.then(
function (result) {
var responseData = {};
console.log('Job Status Response received');
if (result != null) {
//Without the .catch below an error here will be 'silent'
console.log('jobStatus result: ' + util.inspect(result, false, null));
responseData['status'] = 'OK';
responseData['progress'] = result.progress;
res.json(responseData);
} else {
console.log('jobStatus Error');
responseData['status'] = 'Error';
}
res.json(responseData);
console.log('jobStatus Response data sent');
},
function (error) {
console.log('Error while getting job status:', error);
res.json("Error");
})
.catch(function(err) {
//handle errors
console.log('Promise error while getting job status:', err);
});
}

关于node.js - 在 node.js 中使用 express undefined variable 不会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206091/

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