fbmessenger-node icon indicating copy to clipboard operation
fbmessenger-node copied to clipboard

checking if entry is iterable before looping over it

Open aramix opened this issue 6 years ago • 5 comments

aramix avatar Mar 26 '18 11:03 aramix

I'm curious as to why you need to do this? As per the docs this will always be an

Array containing one messaging object

https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/#payload

rickydunlop avatar Mar 26 '18 17:03 rickydunlop

Closing this as it's unnecessary

rickydunlop avatar May 30 '18 10:05 rickydunlop

@rickydunlop sorry just noticed this. I was actually not getting an array in some cases, that's why I added this check to avoid problems when trying to loop over a non-array values. I'll add an example tomorrow.

aramix avatar May 31 '18 23:05 aramix

@rickydunlop even with the latest v5.0.1 I am still getting the error that made me add a check for it. I have this part:

postRoutes.route("/webhook", function(params, req, res, next) {
  res.statusCode = 200;
  // console.log(JSON.stringify(req.body));
  // {"object":"page","entry":[{"id":"1961252584091590","time":1527918002661,"standby":[{"recipient":{"id":"1961252584091590"},"timestamp":1527918002661,"sender":{"id":"1705001502946197"},"postback":{"title":"Who's nearby"}}]}]}
  // as you can see in the above response there is no "messaging" array, so when it gets passed
  // to fbmessenger's handle method it tries to loop over a nonexistent key and crashes.
  cbot.handle(req.body); // <------------------- Error is being thrown here (server/router.js:98:8)
  res.end();
  // DEBUG
  console.log("END POST ROUTE");
});

where cbot is fbmessenger error stacktrace:

2018-06-02T05:14:48.877175+00:00 app[web.1]: TypeError: Cannot read property 'forEach' of undefined
2018-06-02T05:14:48.877177+00:00 app[web.1]:     at entries.forEach.entry (/app/.meteor/heroku_build/app/programs/server/npm/node_modules/fbmessenger/dist/Messenger.js:50:23)
2018-06-02T05:14:48.877179+00:00 app[web.1]:     at Array.forEach (<anonymous>)
2018-06-02T05:14:48.877187+00:00 app[web.1]:     at Messenger.handle (/app/.meteor/heroku_build/app/programs/server/npm/node_modules/fbmessenger/dist/Messenger.js:49:13)
2018-06-02T05:14:48.877189+00:00 app[web.1]:     at server/router.js:98:8
2018-06-02T05:14:48.877191+00:00 app[web.1]:     at doCall (packages/meteorhacks_picker.js:128:14)
2018-06-02T05:14:48.877193+00:00 app[web.1]:     at PickerImp._processRoute (packages/meteorhacks_picker.js:122:5)
2018-06-02T05:14:48.877195+00:00 app[web.1]:     at processNextRoute (packages/meteorhacks_picker.js:90:14)
2018-06-02T05:14:48.877197+00:00 app[web.1]:     at processNextMiddleware (packages/meteorhacks_picker.js:78:7)
2018-06-02T05:14:48.877198+00:00 app[web.1]:     at PickerImp._dispatch (packages/meteorhacks_picker.js:72:3)
2018-06-02T05:14:48.877200+00:00 app[web.1]:     at processNextSubRouter (packages/meteorhacks_picker.js:102:17)
2018-06-02T05:14:48.877202+00:00 app[web.1]:     at PickerImp._dispatch (packages/meteorhacks_picker.js:68:14)
2018-06-02T05:14:48.877203+00:00 app[web.1]:     at processNextSubRouter (packages/meteorhacks_picker.js:102:17)
2018-06-02T05:14:48.877205+00:00 app[web.1]:     at processNextRoute (packages/meteorhacks_picker.js:95:7)
2018-06-02T05:14:48.877206+00:00 app[web.1]:     at processNextMiddleware (packages/meteorhacks_picker.js:78:7)
2018-06-02T05:14:48.877208+00:00 app[web.1]:     at urlencodedParser (/app/.meteor/heroku_build/app/programs/server/npm/node_modules/body-parser/lib/types/urlencoded.js:82:7)

aramix avatar Jun 02 '18 05:06 aramix

Hi @aramix, Thanks for the stacktrace, it has helped me track the issue. It looks like you're using the handover protocol is that correct? If so, it's currently unsupported in this lib but should be easy to add support for it

rickydunlop avatar Jun 04 '18 08:06 rickydunlop