并非所有代码路径都返回值(对于可调用的Google Cloud函数) [英] Not all code paths return a value (for google cloud function callable)

查看:92
本文介绍了并非所有代码路径都返回值(对于可调用的Google Cloud函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个云函数,当我使用一些异步函数并在每个可能的输出中添加"return"时,我仍然得到并非所有代码路径都返回一个值

I have a cloud function and when I use some async functions and put a 'return' in each possible output, I still get Not all code paths return a value

我尝试删除我的数据库调用,只是想让'return {data:{...}};'从而使错误消失.

I've tried deleting my database calls and just having the 'return {data:{...}};' which makes the error go away.

我也尝试过将所有内容包装在"try""catch"块中.

I have also tried wrapping everything inside a 'try' 'catch' block.

我目前希望能工作的是两个块get().then()... catch()..

I current have what I would expect to work which is two blocks 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 deploy 部署我的云功能,相反,我会遇到部署错误:

I would expect to be able to deploy my cloud function with firebase deploy, instead I am getting deploy errors:


src/index.ts:83:62 - error TS7030: Not all code paths return a value.

83 export const getUsersInHere = functions.https.onCall((data, context) =>

注意我想我发现将异步"添加到可调用对象的签名中时,"firestore部署"有效,但是警告/错误"仍然保留在Microsoft Studio代码中(并非所有代码路径都返回value.ts(7030))

Note I think I've found that 'firestore deploy' works when I added 'async' into the signature of the callable, however the 'warning/error' still remains in Microsoft Studio Code (Not all code paths return a value.ts(7030))

export const getUsersInThisChatRoom = functions.https.onCall( async (data,context)=>

export const getUsersInThisChatRoom = functions.https.onCall(async (data, context) =>

推荐答案

使用可调用对象,您可以直接返回要序列化的对象并发送给客户端,也可以返回与要发送的对象一起解析的Promise.您要做的就是在 else if 块中返回承诺:

With callables, you can either directly return the object to be serialized and sent to the client, or you can return a promise that resolves with the object to send. All you have to do is return the promise in your else if block:

    // 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 回调返回的值.

This will return the promise that resolves to either value you returned from the then or catch callback.

您没有义务使用async/await,但如果这样做,则应完全替换then,然后使用正确的async/await语法捕获块.看起来会非常不同.

You have no obligation to use async/await, but if you do, you should completely replace your then and catch blocks with proper async/await syntax. It will look very different.

这篇关于并非所有代码路径都返回值(对于可调用的Google Cloud函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆