Node.js 请求 - 获取响应值无效
Node.js request - Fetching response values not working
Node.js 请求 - 获取响应值无效
我正在使用 node.js 和请求,但我无法获取所需的值 'name'。我的设置中缺少什么?
request.get({
url: "http://127.0.0.1:4004/api/search?name=peter"
}, (error, response, body) => {
if(error) {
console.log(error);
return console.dir(error);
} else {
console.log(response.body);
var getBody = JSON.parse(response.body);
if (getBody.status == 200) {
console.log('Response: '+getBody);
//console.log('Records: '+getBody.records.name[0]);
console.log('Records: '+getBody.records.name);
res.render('profile', {
page:'Profile',
menuId:'profile',
data: {},
errors: {}
});
}
}
});
Console.log:
{"status":200,"error":null,"records":[{"id":235,"abc":"235980JFD","name":"Peter Pan"}]}
Response: [object Object]
Records: undefined
records 是一个数组,因此您必须使用:
console.log('Records: '+getBody.records[0].name);
Node.js 请求 - 获取响应值无效
我正在使用 node.js 和请求,但我无法获取所需的值 'name'。我的设置中缺少什么?
request.get({
url: "http://127.0.0.1:4004/api/search?name=peter"
}, (error, response, body) => {
if(error) {
console.log(error);
return console.dir(error);
} else {
console.log(response.body);
var getBody = JSON.parse(response.body);
if (getBody.status == 200) {
console.log('Response: '+getBody);
//console.log('Records: '+getBody.records.name[0]);
console.log('Records: '+getBody.records.name);
res.render('profile', {
page:'Profile',
menuId:'profile',
data: {},
errors: {}
});
}
}
});
Console.log:
{"status":200,"error":null,"records":[{"id":235,"abc":"235980JFD","name":"Peter Pan"}]}
Response: [object Object]
Records: undefined
records 是一个数组,因此您必须使用:
console.log('Records: '+getBody.records[0].name);