我可能会在某些时候遗漏一些东西,但我不太明白为什么它会起作用:
app.post('/check-setup-auth', function(req, res) {
db.isAuthorizedToInstall()
.then(function(data) {
res.status(200).json(data);
});
});
然而这并不
app.post('/check-setup-auth', function(req, res) {
db.isAuthorizedToInstall()
.then(res.status(200).json);
});
我期望这会产生完全相同的结果,但不知何故,来自 Promise 的解析数据没有传递给 res.json 函数;或者该函数没有被调用。
我正在使用:
- Linux 3.19.0-31-generic#36-Ubuntu x86_64 x86_64 x86_64 GNU/Linux
- nodejs v4.1.1
- express 2.14.4
最佳答案
这两个代码之间有很大差异。
在第一个代码中,res.status(200)
在 db.isAuthorizedToInstall()
结束后执行。
在第二个代码中,res.status(200)
在 db.isAuthorizedToInstall()
结束之前执行。
res.status(200)
开始写入响应 header 。这样可以防止在响应中写入一些其他代码。
我认为该函数是在第二个代码中调用的,但响应的行为并不相同。所以你可能有一些其他代码破坏了这个。
关于javascript - 未使用已定义的函数调用 Node q Promise 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33348258/