我有一个用 Node-js 编写的 Google Cloud 函数,每次有人提交Gupshup 的无服务器 webview 表单时都会调用它
我希望在我的网络服务中收到以下输入:
{
"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"
}
但是我无法将对象放入“有效负载”、时间和用户 ID 对象中。
这是我的代码:
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(req.body.linkid); // undefined
console.log(req.body.payload[0].fieldname); // cannot read property from undefined error
console.log(req.body.time); //undefined
console.log(req.body.userid); // undefined
// 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\":\"f42414e2-ce1a-4bf5-b40a-e88e4d4d9aee\",\"payload\":":{"{\"fieldname\":\"account\",\"fieldvalue\":\"savings\"},{\"fieldname\":\"name\",\"fieldvalue\":\"Alice\"},{\"fieldname\":\"gender\",\"fieldvalue\":\"Male\"},{\"fieldname\":\"interest\",\"fieldvalue\":\"Cooking\"}":""}}
res.sendStatus(200);
};
如您所见,stringify 允许查看所有有效负载属性,但在此之前我无法在 js 对象中访问它们。
第二个问题是字符串化后的事件我看不到时间和用户ID。
我怀疑我必须以不同于我习惯的方式处理 content-type="application/x-www-form-urlencoded" 的请求,但我找不到任何示例。