【问题标题】:How to deal with the different type of errors that returns from the cloud function?如何处理从云函数返回的不同类型的错误?
【发布时间】:2019-07-22 14:25:55
【问题描述】:

我已经编写了云函数,它使用状态码 200 或 400 发送响应,但有时我会收到此错误

函数执行耗时 219 毫秒,完成状态为:“连接错误” 错误:函数在请求范围外崩溃 函数调用被中断。

所以问题是我需要知道发送这个带有一些状态码的错误消息作为云函数的响应。

const functions = require('firebase-functions');
const dialogflow = require('dialogflow');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
admin.initializeApp();
var db = admin.firestore();
const {WebhookClient} = require('dialogflow-fulfillment');
var UID = new Object();

exports.fulfillmenttext = functions.https.onRequest((req,res) =>{ 
    const answer1 = req.body.Text;
    const uid = answer1.substring(0,28);
    const answer = answer1.substring(28);
    const sessionId = uid;
    var count,questvalue;
    const promise = db.collection("***").doc('***').collection("**").doc("uid").get();
    promise.then(doc => {
        snapshot.forEach(function(doc) {
            if (doc.exists) {
              count = doc.data().count;
                if(count == 1){
                    var updatequest = title[questvalue];
                    res.status(200).send({"question":updatequest,"question_number":questvalue});
                    return;
              }
              else{
                runSample();
              }
          } 
          else {
              console.log("No such document!");
          }
        });
      }).catch(function(error) {
          console.log("Error getting document:", error);
      });


    async function runSample() {
      const languageCode = 'en-US';
      const projectId = 'xxxxxxx';
      const credentials = {
        client_email: 'xxxxxxx',
        private_key:
          'xxxxxxx',
      };
      //Instantiate a DialogFlow client.
      const dialogflow = require('dialogflow');
      const sessionClient = new dialogflow.SessionsClient({
        projectId,
        credentials,
      });
    // Define session path
      const sessionPath = sessionClient.sessionPath(projectId, sessionId);
      // The text query request.

        const request = {
            //session: context1,
            session: sessionPath,
            queryInput: {
              text: {
                text: answer,
                languageCode,
              },
            },
        };

      const responses =  await sessionClient.detectIntent(request);
      const result = responses[0].queryResult;
      let action = result.action; 
      if (result.intent) {
        const question = result.fulfillmentText;
        console.log("question is",question);
        const actionHandlers = {
            'early': () => {
                console.log('earlyaction1', action);
                let name1 = JSON.stringify(result.parameters.fields.Name.stringValue);
                name1 = name1.toString().replace(/"/g,"");
                var data1 = {
                    Name: name1
                };
                var setDoc1 = admin.firestore().collection('**').doc('uid').collection("***").doc('uid').collection('**').doc('**').update(data1);


            },

        };
        if (action === 'early') {
             console.log('1');
             actionHandlers[action]();
        }


        res.status(200).send({"question":result.fulfillmentText,"action":action,"question_number":title.indexOf(question)});
        } else {
          console.log(`  No intent matched.`);
          res.status(400).send({"action":"empty"});
      }
    } 

});

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-firestore dialogflow-es


    【解决方案1】:

    该消息告诉您,在函数通过向客户端发送响应正常终止后,您有一些仍在运行的异步代码。那段异步代码崩溃了。由于该函数已通过发送响应终止,因此您无法发送另一个响应。每次函数调用只能有一个响应。

    您需要做的是检查您的代码并确保您 1) 正确处理 Promise 以及 2) 在发送响应后没有故意尝试留下任何工作,因为 Cloud Functions 不支持这一点。

    【讨论】:

    • 是的,我已经使用了异步代码,并且我尝试正确地使用承诺,但问题仍然存在。我的意思是我们是否将该连接错误消息作为带有状态码的响应发送。
    • 由于我们看不到您的代码,因此我们有办法确定您所说的一切都是正确的。
    • 好的,我已经把代码发给你了。- Doug Stevenson
    • 您没有正确处理来自 Firestore API 调用的所有承诺。
    • 您在 runSample 中对 Firestore 文档调用更新,但忽略了它返回的承诺。
    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2020-05-09
    • 1970-01-01
    相关资源
    最近更新 更多