返回值是空时,返回值是放置在.then函数?



在过去的两天里,我一直在试图修复以下问题。我想创建一个用户,然后返回一个列表,如下所示:

exports.createUser = functions.https.onCall((data,context) => {
try {
admin.auth().createUser({
email: "kevin3928@gmail.com"
password: "a3tbmz"
}).then((arg) => {
// a) if I return a variable here it doesn't work
});
// b) if I return a variable here it works
}
});

使用方法b返回一个变量)它工作,但我需要使用方法a)返回变量。我不能使用asyncawait,因为我会收到以下错误:

error  Parsing error: Unexpected token =>
✖ 1 problem (1 error, 0 warnings)

如果有人能帮我解决这个问题,我很感激,我花了整个周末的时间来解决这个问题。

您需要从函数的顶层代码返回一个值,否则Cloud Functions可能会在异步操作完成之前终止您的代码。

exports.createUser = functions.https.onCall((data,context) => {
return admin.auth().createUser({
email: "kevin3928@gmail.com"
password: "a3tbmz"
}).then((arg) => {
return "result";
}).catch((err) => {
return err;
});
});

也请参阅文档中关于返回结果的第二个代码片段,它做了类似的事情。

try

let userCreated = false
exports.createUser = functions.https.onCall((data,context) => {
try {
admin.auth().createUser({
email: "kevin3928@gmail.com"
password: "a3tbmz"
})
userCreated = true
if(userCreated){
return // the object you wanted to return
}
} catch(err){
throw new Error(err)
}
});

最新更新