首页 > 解决方案 > Firebase Firestore 错误:Dialogflow 参数中的“无效使用类型‘未定义’作为 Firestore 参数”

问题描述

我有一个在 Dialogflow 中制作的聊天机器人,我正在尝试按照本教程将用户数据添加到 Firestore 。它不适用于我的参数,即使在尝试定义其他上下文或其他方式来获取参数时也是如此。

当我尝试打印完整的输出以捕获未定义的值时(如此所述),Firebase 只会打印“未定义”。我在这里错过了什么吗?

在此处输入图像描述

我在 Dialogflow Fulfillment webhook 上的 index.js:

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function getNameHandler(agent) {
    let email = agent.parameters.email; 

    db.collection("users").add({ name: email });

    agent.add(`Thank you, $email`);
  }

  let intentMap = new Map();
  intentMap.set('Final', getNameHandler);
  agent.handleRequest(intentMap);
});

包.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.5.0",
    "firebase-admin": "^8.2.0",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.6.1"
  }
}

我还发布了完整的日志,因为人们在与此相关的问题中询问他们。

Firebase 日志错误:

Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field name).
    at Object.validateUserInput (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:273:15)
    at Object.validateDocumentData (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:611:22)
    at CollectionReference.add (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1765:23)
    at getNameHandler (/srv/index.js:17:28)
    at WebhookClient.handleRequest (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:313:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/srv/index.js:24:9)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /worker/worker.js:783:7
    at /worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:132:7)

Dialogflow 原始交互日志:

{
  "queryText": "FACEBOOK_MEDIA",
  "action": "TrabalharAqui.TrabalharAqui-custom.TrabalharAqui-custom-custom.Nome-custom.E-mail-custom.Subir1-ajustado-yes.Subir1-ajustado-yes-custom",
  "parameters": {},
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          ""
        ]
      },
      "lang": "pt-br"
    }
  ],
  "outputContexts": [
    {
      "name": "generic",
      "lifespanCount": 4,
      "parameters": {
        "facebook_sender_id": "3142211899182443",
        "person": {
          "name": "Felipe"
        },
        "person.original": "Felipe",
        "email": "email@email.com",
        "email.original": "email@email.com"
      }
    },
    {
      "name": "nome-followup",
      "lifespanCount": 11,
      "parameters": {
        "person": {
          "name": "Felipe"
        },
        "person.original": "Felipe",
        "email": "email@email.com",
        "email.original": "email@email.com"
      }
    },
    {
      "name": "trabalharaqui-followup",
      "lifespanCount": 4,
      "parameters": {
        "person": {
          "name": "Felipe"
        },
        "person.original": "Felipe",
        "email": "email@email.com",
        "email.original": "email@email.com"
      }
    },
    {
      "name": "subir1-ajustado-yes-followup",
      "lifespanCount": 1
    },
    {
      "name": "trabalharaqui-custom-custom-followup",
      "lifespanCount": 11,
      "parameters": {
        "person": {
          "name": "Felipe"
        },
        "person.original": "Felipe",
        "email": "email@email.com",
        "email.original": "email@email.com"
      }
    },
    {
      "name": "e-mail-followup",
      "lifespanCount": 12,
      "parameters": {
        "person": {
          "name": "Felipe"
        },
        "person.original": "Felipe",
        "email": "email@email.com",
        "email.original": "email@email.com"
      }
    },
    {
      "name": "subir1-followup-2",
      "lifespanCount": 3,
      "parameters": {
        "email": "email@email.com",
        "email.original": "email@email.com"
      }
    }
  ],
  "intent": {
    "id": "96c6e1f9-1682-42c4-bda8-3e02107bef90",
    "displayName": "Final",
    "priority": 500000,
    "webhookState": "WEBHOOK_STATE_ENABLED",
    "inputContextNames": [
      "Subir1-ajustado-yes-followup"
    ],
    "events": [
      "FACEBOOK_MEDIA"
    ],
    "action": "TrabalharAqui.TrabalharAqui-custom.TrabalharAqui-custom-custom.Nome-custom.E-mail-custom.Subir1-ajustado-yes.Subir1-ajustado-yes-custom",
    "messages": [
      {
        "text": {},
        "lang": "pt-br"
      }
    ],
    "endInteraction": true,
    "rootFollowupIntentId": "c4b6c438-41bf-431b-9e46-9d53bcf11d3f",
    "parentFollowupIntentId": "01f5cff4-722c-476e-9520-e84224f83037"
  },
  "intentDetectionConfidence": 1,
  "diagnosticInfo": {
    "webhook_latency_ms": 386,
    "end_conversation": true
  },
  "languageCode": "pt-br",
  "slotfillingMetadata": {
    "allRequiredParamsPresent": true
  },
  "id": "635f4b23-5e81-42dc-aa33-eefdaadca254-36cd004f",
  "sessionId": "56a16be0-0657-4871-84fa-14f9a8f2d524",
  "timestamp": "2019-10-30T20:50:13.307Z",
  "source": "agent",
  "webhookStatus": {
    "webhookEnabledForAgent": true,
    "webhookStatus": {
      "code": 13,
      "message": "Webhook call failed. Error: 500 Internal Server Error."
    }
  },
  "agentEnvironmentId": {
    "agentId": "f86a3353-f332-4a58-b90a-4f2f7f1c76f5",
    "cloudProjectId": "small-talk-c7def"
  }
}

Stackdriver 日志

Dialogflow fulfillment request : { "responseId": "635f4b23-5e81-42dc-aa33-eefdaadca254-36cd004f", "queryResult": { "queryText": "FACEBOOK_MEDIA", "action": "TrabalharAqui.TrabalharAqui-custom.TrabalharAqui-custom-custom.Nome-custom.E-mail-custom.Subir1-ajustado-yes.Subir1-ajustado-yes-custom", "parameters": { }, "allRequiredParamsPresent": true, "fulfillmentMessages": [{ "text": { "text": [""] } }], "outputContexts": [{ "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/generic", "lifespanCount": 4, "parameters": { "facebook_sender_id": "3142211899182443", "agenciaArea": "", "agenciaArea.original": "", "person": { "name": "Felipe" }, "person.original": "Felipe", "email": "email@email.com", "email.original": "email@email.com" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/nome-followup", "lifespanCount": 11, "parameters": { "person": { "name": "Felipe" }, "person.original": "Felipe", "email": "email@email.com", "email.original": "email@email.com", "agenciaArea": "", "agenciaArea.original": "" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/trabalharaqui-followup", "lifespanCount": 4, "parameters": { "agenciaArea": "", "agenciaArea.original": "", "person": { "name": "Felipe" }, "person.original": "Felipe", "email": "email@email.com", "email.original": "email@email.com" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/subir1-ajustado-yes-followup", "lifespanCount": 1 }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/trabalharaqui-custom-custom-followup", "lifespanCount": 11, "parameters": { "person": { "name": "Felipe" }, "person.original": "Felipe", "email": "email@email.com", "email.original": "email@email.com", "agenciaArea": "", "agenciaArea.original": "" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/subir1-ajustado-followup", "parameters": { "email": "email@email.com", "email.original": "email@email.com" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/e-mail-followup", "lifespanCount": 12, "parameters": { "person": { "name": "Felipe" }, "person.original": "Felipe", "email": "email@email.com", "email.original": "email@email.com", "agenciaArea": "", "agenciaArea.original": "" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/subir1-followup-2", "lifespanCount": 3, "parameters": { "email": "email@email.com", "email.original": "email@email.com" } }, { "name": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524/contexts/facebook_media" }], "intent": { "name": "projects/small-talk-c7def/agent/intents/96c6e1f9-1682-42c4-bda8-3e02107bef90", "displayName": "Final", "endInteraction": true }, "intentDetectionConfidence": 1.0, "languageCode": "pt-br" }, "originalDetectIntentRequest": { "source": "facebook", "payload": { "data": { "recipient": { "id": "120676054689749" }, "message": { "attachments": [{ "payload": { "url": "https://cdn.fbsbx.com/v/t59.2708-21/72489391_411292759506400_2547957410975186944_n.txt/bot.txt?_nc_cat\u003d108\u0026_nc_oc\u003dAQnBAGuFxa0u-W4NRK5FPH4gZdq1MwpjgQjlC-pfbSGY7UkdfyRA18aSYMUyX5tN0lTqqYPU8sW6gTg4yTwn0vgK\u0026_nc_ht\u003dcdn.fbsbx.com\u0026oh\u003db322ef1af5154b43139f0a133d2468d9\u0026oe\u003d5DBCB98C" }, "type": "file" }], "mid": "m_2ZRrlcE88CIvxNwgMAZiNRwv8VxAlOHyWofyE4wHB2fGmqMtrlfKMYv_GbDLFUDpGydFktXmGRB29BfVbwxqAg" }, "timestamp": 1.572468612843E12, "sender": { "id": "3142211899182443" } }, "source": "facebook" } }, "session": "projects/small-talk-c7def/agent/sessions/56a16be0-0657-4871-84fa-14f9a8f2d524" }

标签: node.jsfirebasegoogle-cloud-firestoredialogflow-esdialogflow-es-fulfillment

解决方案


错误消息(阅读全文):

错误:参数“data”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值(在字段名称中找到)。

告诉您您为名为“name”的字段传递的值未定义。鉴于这些代码行:

let email = agent.parameters.email; 
db.collection("users").add({ name: email });

断言是email未定义的。所以,你必须弄清楚为什么会这样。


推荐阅读