【发布时间】:2016-10-23 19:58:42
【问题描述】:
我正在使用正文解析器来解析 POST 消息中传入的 JSON 对象。我想将 JSON 中的特定值存储到一个变量中,以便稍后发送到数据库。
这是一个sn-p:
var http = require('http');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Required to process the HTTP body.
// req.body has the Object while req.rawBody has the JSON string.
app.use(bodyParser.json()); // for parsing application/json
app.post('/', function(req, res){
var tropo = new TropoWebAPI();
parameters = req.body['session']['parameters'];
callerID = req.body['session']['from']['id'];
console.log(callerID);
if(callerID = 1234567)
{
\\Intentionally kept out
}
但是,它失败并出现此类型错误:无法读取未定义的属性 'id'
@malix 这是 JSON 对象:
"session": {
"id": "89c3b5d830dd8bb8b372f802aadbdfc9",
"accountId": "1234567",
"applicationId": "1234567",
"timestamp": "2016-06-23T17:09:48.685Z",
"userType": "HUMAN",
"initialText": null,
"callId": "7ab0b9306af2139a1a2e6cc8b7bd7af9",
"to": {
"id": "408XXXYYYY",
"name": "408XXXYYYY",
"channel": "VOICE",
"network": "SIP"
},
"from": {
"id": "408ZZZAAAA",
"name": "408ZZZAAAA",
"channel": "VOICE",
"network": "SIP"
},
}
我正在尝试提取 408ZZZAAAA
请帮忙。
【问题讨论】:
-
你确定
req.body['session']['from']有属性id吗?你能告诉我req.body.session.from吗? -
确定:{ id: '91XXXXXXXXXX name: '91XXXXXXXXXX channel: 'VOICE', network: 'SIP' }
-
试试
req.body.session.from.name有用吗? -
类似:TypeError:无法读取未定义的属性“名称”
-
"from" 本身就是一个对象。不确定这是否会有所帮助。
标签: javascript json express