[英]Can't show response after post request
I'm having trouble when I try to show the answer of post request with node and request. 当我尝试用节点和请求显示发布请求的答案时,我遇到了麻烦。 I can see the response in the console in service, but it does not arrive into controller..why? 我可以在服务器中看到响应,但它没有进入控制器..为什么? Thaaaanks! Thaaaanks!
Here is the code: 这是代码:
function postData(req, res, next){
eventService.getItems()
.then(response => {
const result = response.body.items
const id = nameFilter.map(task => task.id = null)
for(var i = 16 ; i < nameFilter.length; i++){
eventService.postData(nameFilter[i])
}
})
.then(response => {
console.log(response) // undefined
res.send(response)
})
.catch(error => {
console.log('error')
next(error)
})
}
module.exports = {postData}
service 服务
postData(data) {
return new Promise(function (resolve, reject) {
request.post({
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + '00002'
},
url: 'url ',
json: data
},
function (error, response, body) {
if (error) {
reject(error)
} else {
console.log(response.body) // shows the message
resolve(response.body)
}
});
})
}
}
Let's focus on the following part of the code: 让我们关注代码的以下部分:
.then(response => {
const result = response.body.items
const id = nameFilter.map(task => task.id = null)
for(var i = 16 ; i < nameFilter.length; i++){
eventService.postData(nameFilter[i])
}
})
.then(response => { // the previous `then` didn't return anything so you shouldn't expect any argument to be passed here!
console.log(response) // undefined
res.send(response)
})
response
is available in the context of the first then
but not in the second! response
是在第一的上下文中可用then
但不是在第二! (this is the reason it's undefined
). (这是它undefined
的原因)。
In order to pass it to the second then
you'll need to add return response
after the for-loop of the first then
: 为了将它传递给第二个then
你需要添加return response
后for循环第一的then
:
.then(response => {
const result = response.body.items
const id = nameFilter.map(task => task.id = null)
for(var i = 16 ; i < nameFilter.length; i++){
eventService.postData(nameFilter[i])
}
return response; // <-- pass it here
})
.then(response => {
console.log(response) // and now you'll have it!
res.send(response)
})
You need to return something in every .then
in the Promise if you want to use it in the next. 如果你想在下一个版本中使用它,你需要在Promise中的每个.then
中返回一些东西。
In this case there is not point creating a new .then
and you code can be changed to: 在这种情况下,没有必要创建一个新的.then
,您的代码可以更改为:
function postData(req, res, next){
eventService.getItems()
.then(response => {
const result = response.body.items
const id = nameFilter.map(task => task.id = null)
const promises = []
// Should 16 be here? Seems like an error
for(var i = 16 ; i < nameFilter.length; i++){
promises.push(eventService.postData(nameFilter[i]))
}
// postData returns a promise, so above we build an array of all the promises
// then this line will wait for all of them to complete
return Promise.all(promises)
})
.then(allResults => {
// All results will now be the result of every call to `eventService.postData...`
console.log(allResults)
res.send(allResults)
})
.catch(error => {
console.log('error')
next(error)
})
}
module.exports = {postData}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.