내 문제는 req.body.param이 정의되지 않았지만 body 자체가 요청 된 소스에서 원하는 json을 반환한다는 것입니다.req./body.param이 GET 요청을 통해 정의되지 않은 이유는 무엇입니까?
이것은 req.body.data.length 또는 사용 가능한 id 매개 변수의 수에 액세스해야하기 때문에 반복 작업이 필요하지 않음을 의미합니다. 그게 왜 출력을 테스트하기 위해 반복 횟수를 5로 설정했는지.
프로젝트는 express-generator (body-parser가 설치됨)를 통해 생성됩니다.
이
내가[
{
__v: 0,
id: "0",
appid: 730,
updated_at: "2016-06-05T16:52:39.742Z",
_id: "575458d71d1c41f414e3b29a",
created_at: "2016-06-05T16:52:39.731Z"
},
{
__v: 0,
id: "1",
appid: 730,
updated_at: "2016-06-05T16:52:39.749Z",
_id: "575458d71d1c41f414e3b29b",
created_at: "2016-06-05T16:52:39.737Z"
}, ...
]
을 돌려받을 것입니다 요청 된 API를 JSON의 몸처럼 보이는 방법이다 :이 문제는 this issue라고
router.get('/', function(req, res) {
request.get('url/api/items/all/', function (error, response, body) {
console.log(options);
console.log('###########\n '+ body + '\n');
console.log('error '+error);
console.log('req:body '+req.body.data);
if (error && response.statusCode !== 200) {
// request could not be completed
return res.send(400, {message: 'Something went wrong'});
} else {
// get the count
//console.log('body.id.length: '+req.body.id.length);
//console.log('body.data.length: '+req.body.data.length);
//console.log('data.length: '+data.length);
var iterator = 5;//req.body.data.length; //|| req.body.num_items;
// iterate number of times
async.times(iterator, function(number, next){
console.log('iterating'+ number);
var newItem = Item({
id: number,
market_hash_name: req.body.market_hash_name,
appid: '730',
quantity: req.body.quantity,
price: req.body.price,
median_week: req.body.median_week,
median_month: req.body.median_month,
average_median_week: req.body.average_median_week,
trend_week: req.body.trend_week,
trend_month: req.body.trend_month,
total_sold_week: req.body.total_sold_week,
total_sold_month: req.body.total_sold_month,
updated_at: req.body.updated_at
})
// save andt
// call next (which is callback)
newItem.save(next);
}, function(timesErr, timesResult){
// if something failed, return error
// even if 1 item failed to save, it will return error
if(timesErr) {
return res.send(400, {message: 'Something went wrong'});
}
// send the list of saved items;
// or whatever you want
res.status(200).send(timesResult);
});
}
});
});
{
data: [
{
id: 1,
product_name: "Product1",
app: appName,
quantity: 137360,
price: 0.05,
updated_at: "2016-06-04 23:02:03",
median_week: 0.04,
median_month: 0.05,
},
{
id:2,
...,
}
.....
{
id:142640,
...,
}
}
내 기능
로컬 경로 만 처리하는 명시적인 요청 객체를 통해 해당 매개 변수에 액세스하려고하는 것이 문제라고 생각합니다. request.get이 호출 될 때 우리는 시체를 얻는다. 그래서 우리는 쉽게 기록 할 수 있습니다. json 본체 전체를 검색 할 수 있습니다. 속성에 액세스하는 것은 작동하지 않고 정의되지 않은 상태로 반환됩니다. –
이 질문은 오래된 것입니다. 여기에 같은 문제가있는 사람은 해결책입니다. parson에 액세스 할 수있게되면 json으로 본문 내용을 파싱해야합니다. 좋아요. const content = JSON.parse (body) –