Charca / bootbot

Facebook Messenger Bot Framework for Node.js
MIT License
974 stars 253 forks source link

TypeError: Cannot read property 'forEach' of undefined (Heroku logs) #113

Closed unigazer closed 2 years ago

unigazer commented 6 years ago

Tried this example: https://github.com/Charca/bootbot/blob/master/examples/persistent-menu-example.js

but getting this on logs (Heroku):

TypeError: Cannot read property 'forEach' of undefined
at data.entry.forEach (/app/node_modules/bootbot/lib/BootBot.js:427:23)

My index.js

const disableInput = false;

bot.setPersistentMenu([{
        "title": "More about us",
        "type": "nested",
        "call_to_actions": [{
                "title": "Who are the founders?",
                "type": "postback",
                "payload": "FOUNDERS_INFO"
            },
            {
                "title": "Why was Zamphyr founded?",
                "type": "postback",
                "payload": "WHY_INFO"
            }
        ]
    }
], disableInput);

// Persistent menu 
bot.on('postback:FOUNDERS_INFO', (payload, chat) => {
    chat.say('Lorem ipsum...')
})

bot.on('postback:WHY_INFO', (payload, chat) => {
    chat.say('Lorem ispum...')
})

bot.start(process.env.PORT || 3000);

When it comes to

bot.setPersistentMednu( [ {...} ] );

I even tried using "Postman" to send a POST request with same JSON data (https://developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu), but still getting

TypeError: Cannot read property 'forEach' of undefined
at data.entry.forEach (/app/node_modules/bootbot/lib/BootBot.js:427:23)

UPDATE Ok, after couple of seconds the "Persistent menu" works, but Heroku still logs the same error. Can you check that out, if you have time?

secretrobotron commented 6 years ago

Looks like BootBot.js has a handler that assumes entry.messaging exists:

            handleFacebookData(data) {
              // Iterate over each entry. There may be multiple if batched.
              data.entry.forEach((entry) => {
                // Iterate over each messaging event
:( ===>         entry.messaging.forEach((event) => {
                  ...
                });
              });
            }

Capturing the input, I get an object which has no messaging property:

[ { id: '146158229413601',
    time: 1520018748761,
    standby: [ [Object] ] } ]

I think this problem occurs because you're subscribed to events that the bot isn't equipped to handle by default. It just assumes everything will be one kind of event. Adding if (!entry.messaging) return; at the top of that forEach seems to fix things. Not sure what side effect it has though.

FnTm commented 6 years ago

@VladimirDev93 @secretrobotron This seems to be the same issue that's described in https://github.com/Charca/bootbot/issues/86 . The workaround to stop receiving the error is mentioned in that issue as well.

secretrobotron commented 2 years ago

🎉