actions-on-google / dialogflow-conversation-components-nodejs

Rich Responses sample (using Dialogflow) in Node.js
Apache License 2.0
66 stars 36 forks source link

Error getting documents: Error: Unknown response type: #15

Closed janjay2018 closed 5 years ago

janjay2018 commented 5 years ago

Hi,

I'm trying to read documents in the firestore database from dialogflow, but i keep getting this error Error getting documents: Error: Unknown response type: "{document data}"

I want the document to be a response to the users request, so it would be like user: i want to see this document agent: here is the document agent: Document Information below are the imports

 //these are functionalities that need to be used
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

//initialise DB connection
const admin = require("firebase-admin");

process.env.DEBUG = 'dialogflow:debug*'; // enables lib debugging statements
//configure sdk
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const timestamp = admin.firestore.Timestamp;

and the function to retrieve the data

function getPositiveLogs(agent){
        const dialogflowdoc = db.collection('users').doc('user3').
        collection('positiveLogs').where("date","==",true).limit(2);

        return dialogflowdoc.get().
        then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            if(!doc.exists){
                agent.add('Sorry there do not seem to be any logs to see');
            }else{
                const data= doc.data();
                agent.add(data.date);
            }
            console.log(doc.id, " => ", doc.data());
                });
            })
            .catch(function(error) {
                agent.add("error retrieving documents");
                console.log("Error getting documents: ", error);
            });
    }

I would be grateful to anyone who can tell me where I am going wrong! Thank you


Error getting documents: Error: Unknown response type: "{"user_feeling":"happy","user_activity":"met up with friend","user_reward":"no","date":"2019-01-10T19:06:01.526Z"}" at WebhookClient.addResponse_ (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:287:13) at WebhookClient.add (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:254:12) at /user_code/index.js:185:23 at QuerySnapshot.forEach (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:731:22) at /user_code/index.js:181:23 at process._tickDomainCallback (internal/process/next_tick.js:135:7) 

``
Fleker commented 5 years ago

In the error stacktrace, you should see a line number for the error. That may help in identifying exactly where the problem is originating.

janjay2018 commented 5 years ago

Thank you Fleker!, I've updated the issue with the full error, but I still can't figure out what's causing the error

Fleker commented 5 years ago

Oh I see. Your database call was successful. However, you cannot directly add the document results. The agent.add call expects one of several types of responses, such as a string or a button. Your database response has unexpected fields, like user_activity.

You need to convert your database results into a valid format. There are many ways to do it.

const data = doc.data()
agent.add(`User ${data.user_activity} and felt ${data.user_feeling}`)

The second is more ideal, but the first will at least give you a valid response for quick iterating.

janjay2018 commented 5 years ago

That's great! it works now šŸ‘ Thank you!!

janjay2018 commented 5 years ago

Wait, it seems there's another problem with the code, I would be so grateful if you could perhaps point me in the direction to correct it?

Fleker commented 5 years ago

Sure, I could assist if you have something else.

janjay2018 commented 5 years ago

I updated the code so it looks like so, I want to use the code to iterate through each document in the collection positiveLogs and return the date the log was made as a new suggestion. The error I am getting says there are no defined responses, I have looked over the internet for solutions but I cannot find what I am doing wrong, as I have defined the platform responses for the agent? Any help would be much appreciated!

const dialogflowdoc = db.collection('users').doc('user3').
        collection('positiveLogs').where("date","==",true).limit(2);

        return dialogflowdoc.get().
        then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            if(!doc.exists){
                agent.add('Sorry there do not seem to be any logs to see');
            }else{
                const data= doc.data();
                //agent.add(new Suggestion(`-${data.date}.`));
                agent.add(`-${data.date}.`);
            }
            console.log(doc.id, " => ", doc.data());
                });
            })
            .catch(function(error) {
                agent.add("error retrieving documents");
                console.log("Error getting documents: ", error);
            });

     }

The error I got is shown below,

Error: No responses defined for platform: null at V2Agent.sendResponses_ 
(/user_code/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13) at WebhookClient.send_ 
(/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17) at promise.then 
(/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:316:38) at 
process._tickDomainCallback (internal/process/next_tick.js:135:7) 
Fleker commented 5 years ago

The error seems to be originating from this file.

How are you wrapping this code in a function? The same as before? Are you sure that the Suggestion code is no longer being run?

janjay2018 commented 5 years ago

The code is wrapped exactly the same as before, like so,

function getPositiveLogs(agent){
        const dialogflowdoc = db.collection('users').doc('user3').
            collection('positiveLogs').where("date","==",true);

            return dialogflowdoc.get().
            then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                if(!doc.exists){
                    agent.add('Sorry there do not seem to be any logs to see');
                }else{
                    const data= doc.data();
                    agent.add(new Suggestion(`-${data.date}.`));

                }
                console.log(doc.id, " => ", doc.data());
                    });
                })
                .catch(function(error) {
                    agent.add("error retrieving documents");
                    console.log("Error getting documents: ", error);
                });

     }

i am not sure because especially with this function, when i changed it before to include the two lines

const data = doc.data();
agent.add(`${data.user_activity}`);

the first time I deployed it, the response was, 'not available' it was only the second time i deployed fulfilment, that the function worked and I got the correct response. So I'm not sure if there's a delay in the processing or something? and it very well could be the Suggestion code is still being run, because it seems just for this function dialogflow seems super confused?

Is it possible that I am doing something wrong with the Promise?

Fleker commented 5 years ago

There is a brief period where the previous function is still active, so you may have been running a cached version.

With regards to the use of Suggestions, you may want to check if you're importing everything, such as shown here: https://github.com/dialogflow/fulfillment-webhook-nodejs/blob/master/functions/index.js#L21

janjay2018 commented 5 years ago

I'm sorry for the late reply, I was trying to do everything I could think of to check the Suggestions was working. It is definitely working for other functions, it seems there is just no response being shown for this function only. With the following function, there is no error shown in logs and the console is being updated with the information from the document, i.e.

qpZUzhuRMejIuqPT05Ui => { user_reward: 'no', date: Timestamp { _seconds: 1547247817, _nanoseconds: 469000000 }, user_feeling: 'happy', user_activity: 'met up with friend' } 

but nothing shown in the simulator

this is everything I am importing, I am sure I am importing everything required


 //these are functionalities that need to be used
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

//initialise DB connection
const admin = require("firebase-admin");

process.env.DEBUG = 'dialogflow:debug*'; // enables lib debugging statements
//configure sdk
admin.initializeApp(functions.config().firebase);
admin.firestore().settings({timestampsInSnapshots: true});
const db = admin.firestore();
const timestamp = admin.firestore.Timestamp;

and this is the function now

function readPositiveLogs(agent){
         const documentRef = db.collection('users').doc('user3').
            collection('positiveLogs');

        return documentRef.get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {

                if(querySnapshot.empty){
                    console.log('no documents found');
                }else{
                    const data= doc.data();
                    const timestamp_date = data.date;
                    const string_date = timestamp_date.toDate();
                    agent.add(new Suggestion(`${string_date}`));

                    console.log(doc.id, " => ", doc.data());
                }

                    });
                })
                .catch(function(error) {
                    agent.add("error retrieving documents");
                    console.log("Error getting documents: ", error);
                });

     }

I was wondering if this problem was due to the format of the date being a timestamp? because when I write another value from the log, i.e. the user_activity as a suggestion it comes up fine. I've tried JSON.Stringify however, and it does not seem to fix the problem. Is there anything you could suggest?

Thank you!

Fleker commented 5 years ago

A Firestore data object is not a string nor JSON, and you may want to check those docs to see how to modify it into a string.

janjay2018 commented 5 years ago

ahh yes, I see where I'm going wrong. Thank you Nick!