Node.js 使用云函数调用外部API并创建文档

Node.js 使用云函数调用外部API并创建文档,node.js,firebase,google-cloud-firestore,google-cloud-functions,google-maps-sdk-ios,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,Google Maps Sdk Ios,我正在尝试创建一些云函数来: 使用Axios调用Google Directions API 根据API结果在Firestore创建文档 将文档引用返回到我的iOS应用程序。 (我正在执行Blaze计划,现收现付) 我很难创建以下函数,因为我的Node/JS知识非常基础。 有人能快速看一眼,让我知道我错过了什么吗 注意事项: 代码正在部署到firebase,没有警告和错误。我很确定问题在于我试图回电的方式 提前谢谢 编辑 我对代码做了一些更改,但我的iOS应用程序仍然收到零。 代码仍然没有在fir

我正在尝试创建一些云函数来:

  • 使用Axios调用Google Directions API
  • 根据API结果在Firestore创建文档
  • 将文档引用返回到我的iOS应用程序。 (我正在执行Blaze计划,现收现付)
  • 我很难创建以下函数,因为我的Node/JS知识非常基础。 有人能快速看一眼,让我知道我错过了什么吗

    注意事项: 代码正在部署到firebase,没有警告和错误。我很确定问题在于我试图回电的方式

    提前谢谢

    编辑

    我对代码做了一些更改,但我的iOS应用程序仍然收到零。 代码仍然没有在firestore上创建文档

    const functions = require('firebase-functions');
    const axios = require('axios');
    var admin = require("firebase-admin");
    admin.initializeApp();
    
    
    // Func called by iOS App, If user is auth, call google maps api and use response to create a document at firestore
    exports.getDistanceAndSavePackage = functions.https.onCall((data, context) => {
      if (!context.auth){ return {status: 'error', code: 401, message: 'Not signed in'} }
      const userId = context.auth.uid;
      const startCoordinates = data.startCoords;
      const endCoordinates = data.endCoords;
    
      const pkgDocReference = getGoogleRoute(startCoordinates, endCoordinates, res => {  
        console.log('google cloud function has returned');
        let venueId = userId;
        let distance = res.distance.value;
        let resultStartAdd = res.start_address;
        let resultEndAdd = res.end_address;
    
        const pkgDocRef = createTempPackage(venueId, distance, resultStartAdd, resultEndAdd, resultPkg => {
          return resultPkg
        })
        return pkgDocRef;
      })
      return pkgDocReference;
    });
    
    
    
    //Create Package Document
    function createTempPackage(venueId, distance, startingAddress, endingAddress, callback){
      console.log('Creating temp package');
      const docRef = admin.firestore().doc(`/temp_packages/`)
      docRef.set({ 
        id: docRef.id,
        venue_id: venueId,
        distance: distance,
        starting_address: startingAddress,
        ending_address: endingAddress,
        timestamp: admin.database.ServerValue.TIMESTAMP,
        status: 0
      })
      .then(docRef => {
        console.log('Doc created')
        return callback(docRef);
      }).catch(error => {
        console.log('Error trying to create document')
        return callback(error);
      })
    }
    
    
      //Call Google directions API
      function getGoogleRoute(startCoords, endCoords, callback){
        axios({
          method: 'GET',
          url: 'https://maps.googleapis.com/maps/api/directions/json',
          params: {
              origin: startCoords,
              destination: endCoords,
              key: 'mykey'
          },
        })
          .then(response => {
              let legs = response.data.routes[0].legs[0];
              return callback(legs);
            })
            .catch(error => {
              console.log('Failed calling directions API');
                return callback(new Error("Error getting google directions"))
            })
      }
    

    对于HTTPS触发器,您需要
    返回{status:'OK',code:200,data:json}


    因此它实际上会通过web服务器进行响应。

    对于HTTPS触发器,您需要
    返回{status:'OK',code:200,data:json}


    这样它就可以通过web服务器进行响应。

    我不知道这是否是最终的解决方案,但是代码中有一个错误:

    const docRef=admin.firestore().doc('/temp\u packages/')

    此语句应包含以下错误:

    参数“documentPath”的值必须指向文档,但为“${documentPath}”。您的路径不包含偶数个组件。

    该错误在
    docRef.set
    之前抛出,因此在
    catch
    语句中不会考虑该错误。我试图测试它,但我所有的尝试都以这个错误结束。也许这个错误在您的日志中的某个地方


    我希望这会有帮助

    我不知道这是否是最终解决方案,但代码中有一个错误:

    const docRef=admin.firestore().doc('/temp\u packages/')

    此语句应包含以下错误:

    参数“documentPath”的值必须指向文档,但为“${documentPath}”。您的路径不包含偶数个组件。

    该错误在
    docRef.set
    之前抛出,因此在
    catch
    语句中不会考虑该错误。我试图测试它,但我所有的尝试都以这个错误结束。也许这个错误在您的日志中的某个地方


    我希望这会有帮助

    嘿,Martin,OP似乎正在创建一个,您可以将JSON数据返回给调用者。嘿,Martin和Frank,我试图编辑代码,并根据示例和您的答案进行了一些更改,但我仍然不知道发生了什么。我在iOS应用程序的回调中收到nil,代码仍然没有在firestore上创建文档;这必须是
    数据
    ,而不是
    消息
    ,如图所示。。。这样客户端将收到它期望的格式。嘿,Martin,OP似乎正在创建一个,您可以将JSON数据返回给调用者。嘿,Martin和Frank,我尝试编辑代码,并根据示例和您的答案进行了一些更改,但我仍然不知道发生了什么。我在iOS应用程序的回调中收到nil,代码仍然没有在firestore上创建文档;这必须是
    数据
    ,而不是
    消息
    ,如图所示。。。这样客户端将收到它期望的格式。