Search code examples
node.jsdialogflow-esactions-on-google

Array length is not showing


I am using dialogflow to build a chatbot. Following is a sample question array but the format of the actual and the sample is same:

qstion[{
Question 1, What is 33+32?,
Okay, Question 2, What is 76+32?,
Okay, Last Question, Did you like the game? 
}]

I am using the following code to get the length of the array:

app.intent('First', (conv) => {
    const ans = conv.parameters.any;
    if(ans == 65){
            senddata[0] = qstion[0] + ans;
            conv.add(qstion.length);
            conv.ask(qstion[1]);
            }
    else{
            conv.add('Please enter a valid number');
            conv.ask(qstion[0]);
        }
});

The app crashes here. Can you help me get the length of the array?

Update:

This is how it shows on the log when I use console.log(qstion) :

qstion[
'Question1, What is 33+32',
'Okay, Question2, What is 76+32',
'Okay, Last Question, Did you like the game'
]

Solution

  • As your question has the "dialogflow" tag I assume you are using the actions-on-google library. The problem you're seeing is not that your array length is wrong, but that you're using the wrong method to send a response.

    In actions-on-google, you would call conv.ask or conv.close to ask a question or close the conversation respectively.

    Your code snippet shows conv.add(qstion.length). However, in the library you are using conv.add does not exist, likely the cause of the crash. conv.add only works when using the new Actions Builder and the @assistant/conversation library, both of which aren't compatible with Dialogflow.