这是我尝试部署的谷歌云功能的代码。我收到一条错误消息,指出我的 .then() promise 或不一致。有谁知道我做错了什么?
const admin = require('firebase-admin');
const twilio = require('./twilio');
module.exports = function(req, res) {
if (!req.body.phone) {
return res
.status(422)
.send({ error: 'You must provide a phone number' });
}
const phone = String(req.body.phone).replace(/[^\d]/g, '');
admin
.auth()
.getUser(phone)
.then(userRecord => {
const code = Math.floor((Math.random() * 8999 + 1000));
const message = {
body: 'Your code is ' + code,
to: phone,
from: '+18053167032'
};
const callback = (err) => {
if (err) {
return res.status(422).send(err);
}
admin
.database()
.ref('users/' + phone)
.update(
{ code: code, codeValid: true },
() => { res.send({ success: true }
);
};
twilio.messages.create(message, callback);
})
.catch((err) => {
res.status(422).send({ error: err });
});
}
最佳答案
在我的脑海中,精确地使用大括号来缩进是很困难的,并且为了响应@hanoldaa提到的箭头函数,能够准确地跟踪 userRecord => 函数将在哪里是非常重要的结尾。如果它说你的 .then promise 不一致,那么我会假设你要么在非 promise 对象上调用 .then,要么你没有处理 Unresolved promise 。
Javascript.info 对于全局处理 Unresolved promise 有一个很好的建议,使用:
window.addEventListener('unhandledrejection', function(event) {
// the event object has two special properties:
alert(event.promise); // [object Promise] - the promise that generated the error
alert(event.reason); // Error: Whoops! - the unhandled error object
});
new Promise(function() {
throw new Error("Whoops!");
}); // no catch to handle the error
希望这有帮助!
关于javascript - THEN 语句返回 promise /始终返回和一致返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774509/