node.js - 为什么我的 axios 调用没有返回响应?
问题描述
在下面的代码段中,我使用 axios 调用了一个微服务。
app.get('/api/user/microservice/signin', async (req, res) => {
console.log('USER SIGNIN BEGINS')
try {
const user = await axios.post('http://localhost:4210/usermicroservice/signin', req.body)
console.log('USER CONTROLLER user from mongo ', user)
res.send(user);
} catch (error) {
throw error;
}
});
调用目标函数,从数据库中检索数据,但结果永远不会到达调用函数。他们的系统只是挂起。
被调用函数:
app.post('/usermicroservice/signin', async (req, res) => {
console.log('\n*** USER MICROSERVICE SIGNIN CALLED ***')
let user = await UserMicroservice.signin(res, req.body);
console.log('user from mongo ', user)
// res.status(200).json({user})
return user;
});
UserMicroservice.signin() 返回值:
if (await PasswordUtil.check(user.password, existingUser.password) == true) {
existingUser.token = PasswordUtil.generateAccessToken(existingUser, user.password);
console.log('existingUser token', existingUser.token)
console.log('existingUser', existingUser)
delete existingUser.password;
let credentials = { name: existingUser.name, email: existingUser.email, token: existingUser.token };
return credentials;
}
CORS:
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
next();
})
解决方案
过了一会儿,我们弄清楚了,微服务响应应该是:
app.post('/usermicroservice/signin', async (req, res) => {
console.log('\n*** USER MICROSERVICE SIGNIN CALLED ***')
let user = await UserMicroservice.signin(res, req.body);
console.log('user from mongo ', user)
res.status(200).json({user})
});
推荐阅读
- javascript - 将解析的 JSON 读入数组不起作用?
- javascript - TypeError:上传文件时无法读取Node js中未定义的属性“路径”
- javascript - Protractor browser.executeScript 在参数列表后抛出“SyntaxError: missing )”异常
- spring-boot - 如何为现有的 jhipster 项目重新生成新的 JPA 实体(以及不同数据库模式中的数据库表)?
- macros - 宏sas中无法确定行列
- kubernetes - Kubernetes 作业 pod 已成功完成,但其中一个容器尚未准备好
- apache-kafka - kafka 2.12 版本失败 kafka-logs 不是topic-partition的形式
- javascript - JSDOC:覆盖 typedef 对象属性
- excel - 插入缺失数据
- ios - iphone X 最大加速度计和陀螺仪频率