Divya R
Divya R

Reputation: 149

request.body.result is undefined in firebase code

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

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

    console.log(request.body.result.action);
    console.log(JSON.stringify(request.body.result.action));

    let action = request.body.result.action;
    let query = request.body.result.resolvedQuery;

   const parameters = request.body.result.parameters; 

    const inputContexts = request.body.result.contexts;

    if(action === 'save.name'){

                    admin.firestore().collection('users').add({
                        name: name
                    }).then(ref => {
                        console.log('Added new user');
                    });
    elif
        {
        console.log("Specified action is not enabled");
        }
   }
 });

Here, request.body.result is undefined once I deployed, and which throws a type error in action statements?

I have created a chatbot which collects details from user and stores it in the database, For storing the details, I have used firestore database which throws the above error because request.body.result is undefined.Please help out!

Upvotes: 1

Views: 1092

Answers (1)

Prisoner
Prisoner

Reputation: 50721

You don't include the JSON that you're getting, which would help diagnose the problem, but it seems likely that your code is using the V1 of the fulfillment protocol, but your project is set to V2. While you can switch your project back to V1, it would be better if you rewrote it to use V2.

Under V2, request.body.result has been replaced with request.body.queryResult. While some fields and field names have changed, action should still be the same.

Upvotes: 1

Related Questions