我想返回axios的响应但总是返回的响应是未定义的:
wallet.registerUser=function(data){
axios.post('http://localhost:8080/register',{
phone:data.phone,
password:data.password,
email:data.email
}).then(response =>{
return response.data.message;
console.log(response.data.message);
}).catch(err =>{
console.log(err);
})
}
console.log(wallet.registerUser(data));
Run Code Online (Sandbox Code Playgroud)
控制台始终记录为未定义.他们以任何方式回复此回复.
我正在尝试将 docker 中的 mongodb docker 容器的端口绑定到本地主机。我尝试这样做:
sudo docker run -p 127.0.0.1:27018:27017 mongo:3.2
我可以连接到它,但在 docker 容器中添加的数据库不会暴露在本地。
我什至尝试过绑定0.0.0.0.,192.168.x.x这也是我的 IP 地址而不是127.0.0.1. 在所有情况下,我都没有在 mongo docker 容器中获取数据库
我正在从在端口3000上运行的反应应用程序到在端口8080上运行的节点api进行api调用.我收到的错误是:
XMLHttpRequest无法加载http:// localhost:8080/register.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许来源' http:// localhost:3000 '访问.
我的反应api电话是:
export const addUser=(data) =>{
console.log(data);
return function(dispatch,data){
axios.post('http://localhost:8080/register',{
phone:data.phone,
password:data.password,
first_name:data.fname,
last_name:data.lname,
dob:data.dob,
gender:data.gender
})
.then(response =>{
console.log('data submitted successfully');
dispatch({
type : AddUser,
User : data
})
}).catch(err =>{
console.log(err);
})
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务器端代码在node.js中,我将头文件包含在:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
Run Code Online (Sandbox Code Playgroud)
我仍然无法将数据发布到我的数据库..任何建议都会对我有所帮助.谢谢.