rsProdRecs
. 这个
then
和
catch
module.exports = {
rsProdRecs: (req, res) => {
return rsClient.query(query.one);
// This is redundant, you're not transforming res in any way so this can be omitted.
// .then(res => {
// return res;
// })
// This will cause the promise to return 'undefined' if rsClient.query(...) rejects,
// instead error handling should be done in the calling function.
// .catch(err => console.log(err));
}
};
你可以重构你的路线来正确使用你的
Promise
退货方式:
router.get('/', function (req, res, next) {
// We cannot consume the "data" directly, we must wait for the promise to resolve.
queries.rsProdRecs()
.then((data) => {
console.log(data);
res.send(data);
})
.catch((err) => {
console.log(err);
res.status(500); // insert correct error response here
});
});
或使用
async/await
router.get('/', async function (req, res, next) {
try {
const data = await queries.rsProdRecs();
console.log(data);
res.send(data);
} catch (e) {
console.log(err);
res.status(500);
}
});