Typescript 并非所有代码路径在Firebase云函数中都返回值

Typescript 并非所有代码路径在Firebase云函数中都返回值,typescript,firebase,google-cloud-functions,Typescript,Firebase,Google Cloud Functions,我正在使用TypeScript编写云函数。我想调用另一个第三方API。我已经创建了如下所示的函数 export const postData= functions.https.onRequest((req, response) => { if (req.method !== 'POST') { return response.status(500).json({ message: 'not allowed' }); }

我正在使用TypeScript编写云函数。我想调用另一个第三方API。我已经创建了如下所示的函数

export const postData= functions.https.onRequest((req, response) => {
    if (req.method !== 'POST') {
        return response.status(500).json({
            message: 'not allowed'
        });
    }
    else {
        let phoneNumber = req.query.phoneNumber;
        request('https://api.xyz.com/api/insertData.php?authkey=xxxxxx&userid=' + phoneNumber,
         function (error: any, respon: any, body: any) {
            console.log(body);

        })
        .then(function(xyz:any){
            return response.status(200).json({
                dataPosted: true
            })
        })
        .catch(function(error:any){
            return response.status(200).json({
                dataPosted: false
            })
        })
    }
});
但当我试图部署我的函数时,它会说“并非所有代码路径都返回值”。但我在
if
else
中都返回了响应。
我做错了什么?请帮助

看起来您的else字段已运行完毕,无需等待。在这种情况下,它可能达不到“then”或“catch”。你也许应该等待那个请求?

在我看来,else部分没有返回任何东西。你不应该返回请求结果吗

export const postData= functions.https.onRequest((req, response) => {
    if (req.method !== 'POST') {
        return response.status(500).json({
            message: 'not allowed'
        });
    }
    else {
        let phoneNumber = req.query.phoneNumber;
        return request('https://api.xyz.com/api/insertData.php?authkey=xxxxxx&userid=' + phoneNumber,
         function (error: any, respon: any, body: any) {
            console.log(body);

        })
        .then(function(xyz:any){
            return response.status(200).json({
                dataPosted: true
            })
        })
        .catch(function(error:any){
            return response.status(200).json({
                dataPosted: false
            })
        })
    }
});

您正在使用的请求库本机支持回调接口,但不返回承诺

您可以使用
request promise
()和
rp()
方法“返回常规承诺/a+合规承诺”,然后按如下方式调整代码:

//......
import * as rp from 'request-promise';

export const postData = functions.https.onRequest((req, response) => {
  if (req.method !== 'POST') {
    return response.status(500).send('not allowed');
  } else {
    let phoneNumber = req.query.phoneNumber;

    var options = {
      url:
        'https://api.xyz.com/api/insertData.php?authkey=xxxxxx&userid=' +
        phoneNumber,
      method: 'POST'
    };

    return rp(options)
      .then(function(parsedBody: any) {
        response.send('data posted');
      })
      .catch(function(error: any) {
        console.log(error);
        response.status(500).send(error);
      });
  }
});

我是TS的初学者,你能帮我写代码吗?你能澄清一下
请求
对象来自哪个模块吗?我不希望同时看到提供的回调和
.then()
调用。无论如何,应该返回值或返回承诺的是yourlese子句。代码不同,在需要的地方添加了一个返回<代码>返回请求(而不仅仅是
请求(
@SushantSomani Hi),您是否有时间查看建议的解决方案?