回调在请求 npm 中不起作用
Callback not working in request npm
我在使用 Request npm 时遇到回调问题。
我的项目是由 Sailsjs 开发的面向用户的 API,另一方面,我在前端使用 reactjs,并且我使用 Request npm 与 API 进行通信。
我在回调方面遇到了一些问题。
我的代码:
handleClick(event) {
var apiBaseUrl = "http://localhost:1337/user";
if (
this.state.first_name.length > 0 &&
this.state.last_name.length > 0 &&
this.state.email.length > 0 &&
this.state.password.length > 0
) {
request.post({url : apiBaseUrl + "/store", form :{
first_name: this.state.first_name,
last_name: this.state.last_name,
email: this.state.email,
password: this.state.password
}}, function(response) {
console.log(response);
if (response.data.code === 200) {
var loginscreen = [];
loginscreen.push(
<Login
parentContext={this}
appContext={this.props.appContext}
key = {rand}
/>
);
var loginmessage = "Not Registered yet.Go to registration";
this.props.parentContext.setState({
loginscreen: loginscreen,
loginmessage: loginmessage,
buttonLabel: "Register",
isLogin: true
});
} else {
console.log("some error ocurred", response.data.code);
}
});
} else {
alert("Input field value is missing");
}
}
post 请求运行良好,当我检查数据库时,我发现用户已保存并且 chrome 中网络 onglet 的响应很好,但是当我这样做时 console.log(response)
总是 null
。不知道是我的回调不对,还是别的原因。
form
http response
variables in response
Error
后端
// Store user
store: function(req, res) {
var params = _.extend(req.query || {}, req.params || {}, req.body || {});
console.log(params);
UserService.store(params, function(err, user) {
if (err) return res.json();
if (!user) return res.json();
return res.send({
"code": 200,
"success": "user registered sucessfully"
});
});
},
你能帮我解决这个问题吗?
其实问题出在你的回调上,回调有两个参数,应该是这样的:
function(error, response) {.....}
error 是第一个参数,响应是第二个参数,你得到 null
因为有 no错误 !并且响应被视为错误对象。
我在使用 Request npm 时遇到回调问题。 我的项目是由 Sailsjs 开发的面向用户的 API,另一方面,我在前端使用 reactjs,并且我使用 Request npm 与 API 进行通信。 我在回调方面遇到了一些问题。
我的代码:
handleClick(event) {
var apiBaseUrl = "http://localhost:1337/user";
if (
this.state.first_name.length > 0 &&
this.state.last_name.length > 0 &&
this.state.email.length > 0 &&
this.state.password.length > 0
) {
request.post({url : apiBaseUrl + "/store", form :{
first_name: this.state.first_name,
last_name: this.state.last_name,
email: this.state.email,
password: this.state.password
}}, function(response) {
console.log(response);
if (response.data.code === 200) {
var loginscreen = [];
loginscreen.push(
<Login
parentContext={this}
appContext={this.props.appContext}
key = {rand}
/>
);
var loginmessage = "Not Registered yet.Go to registration";
this.props.parentContext.setState({
loginscreen: loginscreen,
loginmessage: loginmessage,
buttonLabel: "Register",
isLogin: true
});
} else {
console.log("some error ocurred", response.data.code);
}
});
} else {
alert("Input field value is missing");
}
}
post 请求运行良好,当我检查数据库时,我发现用户已保存并且 chrome 中网络 onglet 的响应很好,但是当我这样做时 console.log(response)
总是 null
。不知道是我的回调不对,还是别的原因。
form
http response
variables in response
Error
后端
// Store user
store: function(req, res) {
var params = _.extend(req.query || {}, req.params || {}, req.body || {});
console.log(params);
UserService.store(params, function(err, user) {
if (err) return res.json();
if (!user) return res.json();
return res.send({
"code": 200,
"success": "user registered sucessfully"
});
});
},
你能帮我解决这个问题吗?
其实问题出在你的回调上,回调有两个参数,应该是这样的:
function(error, response) {.....}
error 是第一个参数,响应是第二个参数,你得到 null
因为有 no错误 !并且响应被视为错误对象。