无法解决承诺拒绝并发送数组作为响应
Unable to resolve promise rejection and send array as response
我正在尝试处理彼此之间的两个查询。
exports.get_users = (req, res) => {
SubscriptionPlan.find()
.then((result) => {
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
let modifiedData = [];
result.forEach(async (data) => {
if (data.processStatus === "active") {
await Users.findById(data.userId).then(
(response) => {
console.log(response)
modifiedData.push(response);
res.json(modifiedData)
}
);
}
});
})
.catch((err) => console.log(err));
};
我的问题是,如果我采用这种方法,则会收到拒绝承诺的错误:
node:15036) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
还有,我在邮递员中收到的 modifiedData
数组的响应,长度为 3 的数组中只有一个对象。我声明 console.log(response)
, returns 3 个对象需要推送到新数组。
如果我像这样更改我的代码来解决拒绝,
exports.get_users = (req, res) => {
SubscriptionPlan.find()
.then((result) => {
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
let modifiedData = [];
result.forEach(async (data) => {
if (data.processStatus === "active") {
await Users.findById(data.userId).then(
(response) => {
console.log(response)
modifiedData.push(response);
}
);
}
});
res.json(modifiedData)
})
.catch((err) => console.log(err));
};
它 returns 我一个空数组作为邮递员的回应。我知道我被 JS 的异步性质所困,但无法解决这个问题。
PS。使用 async-await 对我没有帮助。
任何早期帮助将不胜感激。
result.forEach
returns 承诺数组。您需要使用 Promise.all([])
一次全部承诺
exports.get_users = (req, res) => {
SubscriptionPlan.find().then(async (result) => {
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
let modifiedData = [];
await Promise.all(
result.map(async(data) => {
if (data.processStatus === "active") {
const response = await Users.findById(data.userId);
modifiedData.push(response);
}
})
);
return res.json(modifiedData);
}).catch((err) => console.log(err));
};
或者马上就能找到
exports.get_users = async (req, res) => {
try {
const result = await SubscriptionPlan.find({ processStatus: "active" });
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
const ids = result.map(({ userId }) => userId);
const response = await Users.find({ userId: { $in: ids } });
return res.json(response);
} catch (err) {
console.log(err)
return res.status(400).json({ message: "unable to process" });
}
};
我正在尝试处理彼此之间的两个查询。
exports.get_users = (req, res) => {
SubscriptionPlan.find()
.then((result) => {
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
let modifiedData = [];
result.forEach(async (data) => {
if (data.processStatus === "active") {
await Users.findById(data.userId).then(
(response) => {
console.log(response)
modifiedData.push(response);
res.json(modifiedData)
}
);
}
});
})
.catch((err) => console.log(err));
};
我的问题是,如果我采用这种方法,则会收到拒绝承诺的错误:
node:15036) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
还有,我在邮递员中收到的 modifiedData
数组的响应,长度为 3 的数组中只有一个对象。我声明 console.log(response)
, returns 3 个对象需要推送到新数组。
如果我像这样更改我的代码来解决拒绝,
exports.get_users = (req, res) => {
SubscriptionPlan.find()
.then((result) => {
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
let modifiedData = [];
result.forEach(async (data) => {
if (data.processStatus === "active") {
await Users.findById(data.userId).then(
(response) => {
console.log(response)
modifiedData.push(response);
}
);
}
});
res.json(modifiedData)
})
.catch((err) => console.log(err));
};
它 returns 我一个空数组作为邮递员的回应。我知道我被 JS 的异步性质所困,但无法解决这个问题。
PS。使用 async-await 对我没有帮助。
任何早期帮助将不胜感激。
result.forEach
returns 承诺数组。您需要使用 Promise.all([])
exports.get_users = (req, res) => {
SubscriptionPlan.find().then(async (result) => {
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
let modifiedData = [];
await Promise.all(
result.map(async(data) => {
if (data.processStatus === "active") {
const response = await Users.findById(data.userId);
modifiedData.push(response);
}
})
);
return res.json(modifiedData);
}).catch((err) => console.log(err));
};
或者马上就能找到
exports.get_users = async (req, res) => {
try {
const result = await SubscriptionPlan.find({ processStatus: "active" });
if (!result) {
return res.status(400).json({ message: "unable to process" });
}
const ids = result.map(({ userId }) => userId);
const response = await Users.find({ userId: { $in: ids } });
return res.json(response);
} catch (err) {
console.log(err)
return res.status(400).json({ message: "unable to process" });
}
};