mathew-kurian / FacebookMessengerBot.js

:mailbox: Simple, light-weight Facebook Messenger Bot API for Node with ES6 support (via. Promises)
https://developers.facebook.com/docs/messenger-platform
117 stars 24 forks source link

Quickreplies result in unhandled exception #16

Closed Kilian closed 7 years ago

Kilian commented 7 years ago

Whenever you add quickreplies to a message, you get the following error:

Unhandled rejection TypeError: Cannot read property 'hasOwnProperty' of null
    at Bot._callee7$ (~/node_modules/facebook-messenger-bot/dist/Bot.js:479:62)
    at tryCatch (~/node_modules/regenerator-runtime/runtime.js:63:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (~/node_modules/regenerator-runtime/runtime.js:337:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (~/node_modules/regenerator-runtime/runtime.js:96:21)
    at GeneratorFunctionPrototype.tryCatcher (~/node_modules/bluebird/js/release/util.js:16:23)
    at PromiseSpawn._promiseFulfilled (~/node_modules/bluebird/js/release/generators.js:97:49)
    at Bot.<anonymous> (~/node_modules/bluebird/js/release/generators.js:201:15)
    at Bot.handleMessage (~/node_modules/facebook-messenger-bot/dist/Bot.js:541:23)
    at ~/node_modules/facebook-messenger-bot/src/Bot.js:257:11
    at Layer.handle [as handle_request] (~/node_modules/express/lib/router/layer.js:95:5)
    at next (~/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (~/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (~/node_modules/express/lib/router/layer.js:95:5)
    at ~/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (~/node_modules/express/lib/router/index.js:330:12)
    at next (~/node_modules/express/lib/router/index.js:271:10)

Seems to be caused by the assignment on line 191 of the non-transpiled Bot.js. The problem is that parsing an empty string does not fail, but won't return an object. The fix is to add a default:

      let postback = {};

      try {
        postback = JSON.parse(message.quick_reply.payload) || {};
      } catch (e) {
        // ignore
      }

That way, you know you're dealing with an object.