如何解决 promiss.all() 并想删除一个级别的对象
How to resolve promiss.all() and want to remove a level of object
我想合并两个sequelize数据的结果。所以我只是创建了两个模型的实例,然后立即使用 promise.All() 解析它们。我做了以下事情来达到同样的目的:
let screensP = AppEvent.findAll({
attributes : [
'id',
'title'
],
});
let eventsP = AppScreen.findAll({
attributes : [
'id',
'title'
],
});
exports.settings = function(req, res, next){
Promise.all([{'screens' : screensP, 'events' : eventsP}])
.then((values) => {
res.json({
'status': 200,
'data': values,
});
});
}
但响应包含如下所示的承诺对象
{
"status": 500,
"data": [
{
"screens": {
"isFulfilled": true,
"isRejected": false,
"fulfillmentValue": [
{
"id": 1,
"title": "click"
},
{
"id": 2,
"title": "comment"
}
]
},
"events": {
"isFulfilled": true,
"isRejected": false,
"fulfillmentValue": []
}
}
]
}
我想要这样的结果
{
"status": 200,
"events": [
{
"id": 1,
"title": "dashboard"
},
{
"id": 2,
"title": "profile"
},
{
"id": 3,
"title": "book"
}
],
"screens": []
}
我应该怎么做才能得到上面的回复?
res.json({
'status': 200,
...values,
});
对于 Promise.all()
的调用,您传入一个数组,其中包含一个元素,该元素是一个具有两个属性的对象,每个属性都是一个承诺。这就是您在结果中看到的(isFulfilled
和其他属性)。要解析 promise,您需要传入一个 promise 数组,而不是一个对象。
此示例使用更易于阅读的 async/await
。
exports.settings = async function(req, res) {
// you can deconstruct the returned elements when the promise is resolved
const [ screens, events ] = await Promise.all([
// returns a promise that will resolve "events"
AppEvent.findAll({
attributes : [
'id',
'title'
],
// if you don't need to parse to Instance objects, adding raw:true will be faster
// raw: true,
}),
// returns a promise that will resolve "screens"
AppScreen.findAll({
attributes : [
'id',
'title'
],
// if you don't need to parse to Instance objects, adding raw:true will be faster
})
]);
// return the results
return res.json({
'status': 200,
'data': {
screens,
events
},
});
};
使用 thenables 的相同示例:
exports.settings = function(req, res) {
// you can deconstruct the returned elements when the promise is resolved
return Promise.all([
// returns a promise that will resolve "events"
AppEvent.findAll({
attributes : [
'id',
'title'
],
}),
// returns a promise that will resolve "screens"
AppScreen.findAll({
attributes : [
'id',
'title'
],
})
])
.then(([ screens, events ]) => {
// return the results
return res.json({
'status': 200,
'data': {
screens,
events
},
});
});
};
我想合并两个sequelize数据的结果。所以我只是创建了两个模型的实例,然后立即使用 promise.All() 解析它们。我做了以下事情来达到同样的目的:
let screensP = AppEvent.findAll({
attributes : [
'id',
'title'
],
});
let eventsP = AppScreen.findAll({
attributes : [
'id',
'title'
],
});
exports.settings = function(req, res, next){
Promise.all([{'screens' : screensP, 'events' : eventsP}])
.then((values) => {
res.json({
'status': 200,
'data': values,
});
});
}
但响应包含如下所示的承诺对象
{
"status": 500,
"data": [
{
"screens": {
"isFulfilled": true,
"isRejected": false,
"fulfillmentValue": [
{
"id": 1,
"title": "click"
},
{
"id": 2,
"title": "comment"
}
]
},
"events": {
"isFulfilled": true,
"isRejected": false,
"fulfillmentValue": []
}
}
]
}
我想要这样的结果
{
"status": 200,
"events": [
{
"id": 1,
"title": "dashboard"
},
{
"id": 2,
"title": "profile"
},
{
"id": 3,
"title": "book"
}
],
"screens": []
}
我应该怎么做才能得到上面的回复?
res.json({
'status': 200,
...values,
});
对于 Promise.all()
的调用,您传入一个数组,其中包含一个元素,该元素是一个具有两个属性的对象,每个属性都是一个承诺。这就是您在结果中看到的(isFulfilled
和其他属性)。要解析 promise,您需要传入一个 promise 数组,而不是一个对象。
此示例使用更易于阅读的 async/await
。
exports.settings = async function(req, res) {
// you can deconstruct the returned elements when the promise is resolved
const [ screens, events ] = await Promise.all([
// returns a promise that will resolve "events"
AppEvent.findAll({
attributes : [
'id',
'title'
],
// if you don't need to parse to Instance objects, adding raw:true will be faster
// raw: true,
}),
// returns a promise that will resolve "screens"
AppScreen.findAll({
attributes : [
'id',
'title'
],
// if you don't need to parse to Instance objects, adding raw:true will be faster
})
]);
// return the results
return res.json({
'status': 200,
'data': {
screens,
events
},
});
};
使用 thenables 的相同示例:
exports.settings = function(req, res) {
// you can deconstruct the returned elements when the promise is resolved
return Promise.all([
// returns a promise that will resolve "events"
AppEvent.findAll({
attributes : [
'id',
'title'
],
}),
// returns a promise that will resolve "screens"
AppScreen.findAll({
attributes : [
'id',
'title'
],
})
])
.then(([ screens, events ]) => {
// return the results
return res.json({
'status': 200,
'data': {
screens,
events
},
});
});
};