我以前从未见过这种情况,但我有一个 Express 应用程序正在运行,并且在本地主机端口上 Axios 返回请求没有问题,但在本地主机域上它返回“绑定(bind)的 consoleCall”。有谁有这方面的经验并知道这意味着什么吗?
<meta property="og:title" content="bound consoleCall" />
helpers.js
const axios = require('axios');
const {
titleSuffix,
organizationPath,
varietyPath
} = require('./constants');
let organizationData = {};
let varietyData = {};
const Helpers = {
fetchOrganization: (organizationID) => {
return axios
.get(organizationPath + organizationID)
.then(response => {
const organizationData = response.data.data;
return organizationData
})
.catch(err => console.error);
},
setOrgSocialMeta: (growerHash, res) => {
return Helpers.fetchOrganization(growerHash)
.then(org => {
return org.name + titleSuffix;
});
}
};
module.exports = Helpers;
服务器.js
const {
setOrgSocialMeta,
setVarSocialMeta
} = require('./helpers');
app.get(['/org/:growerHash/*', '/_org/:growerHash/*'], (req, res) => {
setOrgSocialMeta(req.params.growerHash, res)
.then(orgTitle => {
res.locals.meta.title = orgTitle;
res.locals.meta.og.title = orgTitle;
res.render('org');
});
});
最佳答案
这意味着axios请求失败,但失败没有被正确处理;实际错误未知,因为它被抑制了。
.catch(err => console.error)
导致捕获错误并使用 console.error
函数解决,该函数被字符串化为绑定(bind)的 consoleCall
.
应该是
.catch(err => console.error(err))
或
.catch(console.error);
或者,由于拒绝不被视为成功响应,因此 fetchOrganization
中不应有 catch
,但应在路由处理程序中处理错误:
app.get(['/org/:growerHash/*', '/_org/:growerHash/*'], (req, res, next) => {
setOrgSocialMeta(req.params.growerHash, res)
.then(...)
.catch(next); // passed to error-handling middleware
});
关于node.js - 为什么 Axios 在 Express 中返回 "bound consoleCall"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51427136/