1

Intent in question

Error I get This is the only error that I get and it logs in firebase. It does not tell me much so I sent screenshot of the error that I get.

Error: Query.once failed: First argument must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".
    at validateEventType (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1593:19)
    at Reference.Query.once (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4825:9)
    at This (/user_code/index.js:40:5)
    at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:88:9)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /var/tmp/worker/worker.js:724:7
    at /var/tmp/worker/worker.js:707:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

Request Body

Dialogflow Request body:

{
  "responseId": "6c61001c-7b4c-4d1d-9790-6774dde5a821",
  "queryResult": {
    "queryText": "what is the number",
    "action": "This",
    "parameters": {
      "number": "number"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/bot-tmnvld/agent/intents/0f19a151-afbb-4c28-b948-ced1f3b56d6e",
      "displayName": "This"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en-us"
  },
  "originalDetectIntentRequest": {
    "source": "GOOGLE_TELEPHONY",
    "payload": {
      "telephony": {
        "caller_id": "REDACTED_IN_STANDARD_TIER_AGENT"
      }
    }
  },
  "session": "projects/bot-tmnvld/agent/sessions/doP4-dClQBiAl_WDixhMkg",
  "alternativeQueryResults": [
    {
      "queryText": "what is the number",
      "languageCode": "en-us"
    }
  ]
}

Code:

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');

const firebase = require("firebase-admin");

//const serviceAccount = require("path/to/serviceAccountKey.json");
firebase.initializeApp();

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

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

 function Data(agent){
     const allParam = agent.parameters.all;
     const all = allParam.replace(/\+/g, "").replace(/-/g,'').replace(/ /g,'');

     //agent.add(number + number + number);
     return firebase.database().ref('/all').push({all: all}).then((snapshot) => {
     console.log('database write successful: ' + snapshot.ref.toString());
 });
}

function This(agent){

 const db = firebase.database();
 const ref = db.ref("my/database/my/data");
  return ref.once("value") 
 .then( snapshot => {
  console.log(snapshot.val());
    // Don't forget to return something to Dialogflow

}, 
ref.once('unhandledRejection', err => {
    console.log('Unhandled rejection:', err);

}));
}


  let intentMap = new Map();
  //intentMap.set('Default Intent', welcome);
 // intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('Data', Data);
  intentMap.set('This', This);

  agent.handleRequest(intentMap);
});
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • There is an extra closing brace and parenthesis that don't match up. Can you post your entire code? And re-post your full error message? – Prisoner Dec 04 '18 at 17:02
  • I re-posted! The error just said unhandled rejection. Would it also create that error if there is no data to retrieve? – user10741953 Dec 04 '18 at 17:49
  • It should not. (In general, posting screen shots of logs isn't that helpful, as an aside.) I see that you're logging the request body. Can you update the question to include the full text of the body that is being logged (as text), since it might help explain what is going on. – Prisoner Dec 04 '18 at 17:53
  • The "Unhandled rejection" points to you not handling the error condition correctly. See the portion of my answer about Promises. – Prisoner Dec 04 '18 at 17:54
  • I undated my info and added my request body. My data saves with one function and it works but I am trying to retrieve data to send to my intent with a different set of data. The data I am retrieving is in firebase and stored into firebase but is from a different application. – user10741953 Dec 05 '18 at 12:53
  • Have you updated the read to use a promise and to log the error condition? – Prisoner Dec 05 '18 at 13:14
  • I undated the code and added the error – user10741953 Dec 05 '18 at 15:19

1 Answers1

0

The error message suggests that you failed to register this Intent Handler. When using dialogflow-fulfillment, you typically have code such as this to map the Intent name to the function you want to use as the handler for that Intent:

  let intentMap = new Map();
  intentMap.set('IntentName', intentHandler);
  agent.handleRequest(intentMap);

It looks like you need to add such a line for this function.

However, your handler also has other problems:

  • Since you are doing an asynchronous function (it has callbacks), you need to return a Promise.
  • You're also using the on() function, when the once() function is probably more appropriate (you don't want to get updates - you just want a single snapshot of the data).
  • I'm also not sure why you're using "/all" as the first parameter to on(), since that is supposed to be an event type, and typically you use the type of "value". So I'm not sure what you think it is supposed to be doing.

Fortunately, the once() command returns a Promise if you don't use callbacks. Something like this might be more what you want, but it is difficult to tell:

return ref.once("value")
  .then( snapshot => {
    console.log(snapshot.val());
    // Don't forget to return something to Dialogflow
  })
  .catch( err => {
    console.log(err);
    // Dialogflow should say something went wrong
  });

Update: In the error message you just posted, the crucial line is

Query.once failed: First argument must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".

The code you currently have posted does not look syntactically valid, but this line is definitely wrong:

ref.once('unhandledRejection', err => {
    console.log('Unhandled rejection:', err);

})

I'm not sure what that is supposed to be doing (is that supposed to be the .catch() block?) but as the error suggests, you almost certainly want your call to ref.once() to be ref.once('value') as I illustrated in my code above.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • intentMap.set('this', This); agent.handleRequest(intentMap); This is what I have and still get unhandle error – user10741953 Dec 04 '18 at 15:33
  • Adding code in a comment is really difficult to read. Instead, make sure you update the question (in this case with the code from your map as well as a screen shot of the Intent in question) and put a note in the comments saying you've done so. – Prisoner Dec 04 '18 at 15:43
  • Okay I got everything working but have you ever seen an issue with the bot repeating the training question. This is when you ask the training question then the second time it will respond with my function. – user10741953 Dec 09 '18 at 21:38
  • If the answer has helped, accepting and/or upvoting it is always appreciated. If you are seeing other problems, then asking it as a new question with as much detail as you can is probably best. – Prisoner Dec 10 '18 at 01:35