Saturday, 22 April 2017

Cannot read the whole webview callback from Gupshup.io API inside my cloud function

I have a Google Cloud function written in Node-js that gets invoked each time somebody submits a Gupshup's serverless webview form

I expect to receive the following input in my web service:

{
    "linkId": "f42414e2-ce1a-4bf5-b40a-e88e4d4d9aee",
    "payload": [{
                  "fieldname": "name",
                  "fieldvalue": "Alice"
               },{
                   "fieldname": "gender",
                   "fieldvalue": "Male"
               },{
                   "fieldname": "account",
                   "fieldvalue": "savings"
               },{
                   "fieldname": "interest",
                   "fieldvalue": "Cooking"
               }],
    "time": 1479904354249,
    "userid": "UserID"
}

But i'm having trouble getting the objects inside "payload", time and userid objects.

This is my code:

exports.orderForm = (req, res) => {
  const data = req.body;
  const ref = data.userid;
  var propValue;

  console.log(req.method); // POST
  console.log(req.get('content-type')); // application/x-www-form-urlencoded
  console.log(r)
  
  // I attemp to print the properties, but they won't print
  for(var propName in req.body.payload) {
      propValue = req.body.payload[propName];
      console.log(propName, propValue);
  }
  console.log('JSON.stringify: ' + JSON.stringify(req.body)); // This prints the following:
  // JSON.stringify: {"{\"linkId\":\"da291b37-374c-450a-b79a-b0b3b8f3748e\",\"payload\":":{"{\"fieldname\":\"account\",\"fieldvalue\":\"\"},{\"fieldname\":\"date\",\"fieldvalue\":\"\"},{\"fieldname\":\"name\",\"fieldvalue\":\"dssdffds\"},{\"fieldname\":\"textArea\",\"fieldvalue\":\"\"},{\"fieldname\":\"time\",\"fieldvalue\":\"\"}":""}}
  res.sendStatus(200);
};

As you can see stringify allows to see all payload properties, but before that i cannot access them in the js object.

The second problem is that i can't access time and userid.

The only js object i can access is linkid.

What i suspect is i must handle requests of content-type="application/x-www-form-urlencoded" differently from what i'm used to, but i couldn't find any examples for that.



via Ariel

No comments:

Post a Comment