我有一个云函数,当我使用一些异步函数并在每个可能的输出中放一个'return'时,我仍然得到并非所有代码路径都返回一个值
我已经尝试删除我的数据库调用,只是让'return {data:{...}};'这使得错误消失了。
我也尝试将所有内容包装在'try''cat'块中。
我目前有我期望的工作,这是两个块get()。then()... catch()..
export const getUsersInHere = functions.https.onCall((data, context) =>
{
if(!context || !context.auth || !context.auth.uid)
{
return {data:{message:"Please login again...", success:false}};
}
else if(data)
{
const my_uid = context.auth.uid;
db.collection(`InHere/${my_uid}`).get().then(snapshot =>
{
return {data:{}, success:true};
}).catch(e =>
{
return {data:{message:"No last message yet...", success:false}};
});
}
else
{
return {data:{message:"no body sent", success:false}};
}
});
我希望能够使用firebase部署部署我的云功能,而不是我遇到部署错误:
src/index.ts:83:62 - error TS7030: Not all code paths return a value.
83 export const getUsersInHere = functions.https.onCall((data, context) =>
注意我认为我发现'firestore deploy'在我将'async'添加到callable的签名中时起作用,但是'warning / error'仍然保留在Microsoft Studio Code中(并非所有代码路径都返回value.ts( 7030))
export const getUsersInThisChatRoom = functions.https.onCall(async(data,context)=>
使用callables,您可以直接返回要序列化的对象并发送到客户端,也可以返回一个与要发送的对象一起解析的promise。您所要做的就是在else if
区块中返回承诺:
// note the return added to the next line
return db.collection(`InHere/${my_uid}`).get().then(snapshot =>
{
return {data:{}, success:true};
}).catch(e =>
{
return {data:{message:"No last message yet...", success:false}};
});
这将返回将解析为从then
或catch
回调返回的值的promise。
您没有义务使用async / await,但如果这样做,则应该使用正确的async / await语法完全替换then和catch块。它看起来会非常不同。