我正在尝试使用Cloud Function和Twilio实现验证。Twilio工作正常,并发送了带有代码的SMS,但在Flutter应用程序中,我收到了错误[firebase_functions/超过截止日期]deadline_EXCEEDE。我做错了什么?
云功能:
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
exports.sendSms = (req, res) => {
console.log('start sendSms');
var func = client.verify.services('xxxxx')
.verifications
.create({to: req.body.data.phone, channel: 'sms'})
.then(verification => {return verification})
.then(result => {
if (result.status === 'pending')
return {status: 'success', result: "pending"};
else
return {status: 'error', error_code: -1, error_msg: result.status};
})
.catch(e => {
return {status: 'error', error_code: e.code, error_msg: e.message};
});
return Promise.all([func]).then(result => {
console.log(result); // console output [ { status: 'success', result: 'pending' } ]
return result[0];
});
};
颤振:
HttpsCallable callable = FirebaseFunctions.instanceFor(region: 'europe-west3').httpsCallable(
'send-sms',
options: HttpsCallableOptions(timeout: const Duration(seconds: 10))
);
dynamic results = await callable.call(<String, dynamic>{'phone': phoneNumber});
create方法也返回promise,所以您也必须在那里添加return关键字。
代替:
var func = client.verify.services('xxxxx')
试试这个:
exports.sendSms = functions.https.onCall((data, context) => {
//^^^^^^^^^^^^^^^^^^^^^^
console.log('start sendSms');
return client.verify.services('xxxxx')
.verifications
.create({to: data.phone, channel: 'sms'})
.then(verification => {return verification})
.then(result => {
if (result.status === 'pending')
return {status: 'success', result: "pending"};
else
return {status: 'error', error_code: -1, error_msg: result.status};
})
.catch(e => {
return {status: 'error', error_code: e.code, error_msg: e.message};
});
});
也就是说,你不需要这个部分:
return Promise.all([func]).then(result => {
console.log(result); // console output [ { status: 'success', result: 'pending' } ]
return result[0];
});