Node.js 如何将URL中的数组作为Promise.all中的参数传递
如何将Node.js 如何将URL中的数组作为Promise.all中的参数传递,node.js,rest,api,express,promise,Node.js,Rest,Api,Express,Promise,如何将url\u user数组作为Promise.all中的参数传递 我使用Node.js和Express url_project = 'https://api.projects' url_user = [ 'https://api.users/1', 'https://api.users/2', 'https://api.users/3', 'https://api.users/4', 'https://api.users/5', 'https://api.
url\u user
数组作为Promise.all中的参数传递
我使用Node.js和Express
url_project = 'https://api.projects'
url_user = [
'https://api.users/1',
'https://api.users/2',
'https://api.users/3',
'https://api.users/4',
'https://api.users/5',
'https://api.users/6'
]
index.js
<div>
<%= projectsApi.id %>
<% for (var i = 0; i < usersApi.length; i++) { %>
<%= usersApi[i].id %>
<% } %>
</div>
var rp = require('request-promise');
Promise
.all([rp({uri: url_project, json:true}), rp({uri: url_user, json:true})]) //This where I want to edit
.then(([projectsApi, usersApi]) => {
res.render('index', {projectsApi, usersApi});
}).catch(err => {
console.log(err);
res.sendStatus(500);
});
当我把Promise.all([rp({uri:url\u project,json:true})、rp({uri:url\u user[0],json:true})])
放进去时,它会工作,因为url\u user[0]
不是数组
但是,我希望传递数组中的所有数据url\u user
您可以使用,并且:
您可以使用和:
这应该起作用:
const url_project = 'https://api.projects'
const url_user = [
'https://api.users/1',
'https://api.users/2',
'https://api.users/3',
'https://api.users/4',
'https://api.users/5',
'https://api.users/6'
]
const promises = [
rp({uri: url_project, json:true}),
...url_user.map(value => rp({uri: value, json:true})
];
Promise
.all(promises)
.then(([projectsApi, ...usersApi]) => {
res.render('index', {projectsApi, usersApi});
}).catch(err => {
console.log(err);
res.sendStatus(500);
});
如果有任何问题,请告诉我。这应该可以:
const url_project = 'https://api.projects'
const url_user = [
'https://api.users/1',
'https://api.users/2',
'https://api.users/3',
'https://api.users/4',
'https://api.users/5',
'https://api.users/6'
]
const promises = [
rp({uri: url_project, json:true}),
...url_user.map(value => rp({uri: value, json:true})
];
Promise
.all(promises)
.then(([projectsApi, ...usersApi]) => {
res.render('index', {projectsApi, usersApi});
}).catch(err => {
console.log(err);
res.sendStatus(500);
});
如果有任何问题,请告诉我。代码块:
const rp=require('request-promise');
const=require('lodash')//lodash很棒,否则你可以使用JS-map/concat
constprojecturl=https://api.projects';
常量用户URL=[
'https://api.users/1',
...,
...,
];
常量承诺=
_(项目网址)
.concat(用户URL)
.map((requestUrl)->rp({uri:requestUrl,json:true}))
.value()
等待承诺
.所有(承诺)
。然后((结果)=>{
res.render('index',{projectsApi:results.shift(),usersApi:…results});
}).catch(错误=>{
控制台日志(err);
res.sendStatus(500);
});
说明:
Steps:
- Get all requests together
- Map over each using lodash or js map to get promises
- Execute them together
- Since project_url is the first request the result is the first item in array
- Spread the reset of the result to get user results
代码块:
const rp=require('request-promise');
const=require('lodash')//lodash很棒,否则你可以使用JS-map/concat
constprojecturl=https://api.projects';
常量用户URL=[
'https://api.users/1',
...,
...,
];
常量承诺=
_(项目网址)
.concat(用户URL)
.map((requestUrl)->rp({uri:requestUrl,json:true}))
.value()
等待承诺
.所有(承诺)
。然后((结果)=>{
res.render('index',{projectsApi:results.shift(),usersApi:…results});
}).catch(错误=>{
控制台日志(err);
res.sendStatus(500);
});
说明:
Steps:
- Get all requests together
- Map over each using lodash or js map to get promises
- Execute them together
- Since project_url is the first request the result is the first item in array
- Spread the reset of the result to get user results
非常感谢。当我打印出
console.log(usersApi)
时,它显示了不同的JSON数据,这是url信息,但url\u用户数据。谢谢。当我打印出console.log(usersApi)
时,它显示了不同的JSON数据,这是url信息,但url\u用户数据。谢谢。我怎样才能完成这部分代码<代码>res.render('index',{projectsApi:results.shift(),usersApi:…results})代码>什么是响应结构?可能,JSON.stringify结果并粘贴resulturl:{[…]},方法:“GET”,标题:{accept:“application/JSON”}……谢谢。我怎样才能完成这部分代码<代码>res.render('index',{projectsApi:results.shift(),usersApi:…results})代码>什么是响应结构?可能,JSON.stringify结果并粘贴resultur:{[…]},方法:“GET”,标题:{accept:“application/JSON”}…当我打印出console.log(usersApi)
时,它显示了JSON的不同数据,这些数据是url信息,但不是url\u用户数据。你能给我看一下console.log(usersApi)
,或者至少是响应的结构?url:{[…]},方法:“GET”,标题:{accept:“application/json”}…
对于其他上下文,console.log(projectsApi)的输出是什么?嗯,奇怪,它应该可以工作。你能给我看一下你的实际代码吗?当我打印出console.log(usersApi)
时,它显示了不同的JSON数据,这些数据是url信息,但不是url\u用户数据。你能给我看一下console.log(usersApi)
的输出,或者至少是响应的结构吗?url:{[…]},方法:“GET”,标题:{accept:“application/json”}…
对于其他上下文,console.log(projectsApi)
的输出是什么?嗯,奇怪,它应该可以工作。你能给我看看你的实际代码吗?