Node 服务器正在发布到第三方网站并获取响应。
如果响应包含诸如密码不匹配。
之类的内容,服务器将向UI发送诸如400:错误密码。
之类的错误。否则服务器将分析响应并从中发送部分数据。
我无法将errorMessage
发送回UI。如何从anaylzeData方法返回错误对象并将其发送回客户端?
代码:
app.post('/login', [express.urlencoded(), express.json()], function(req, res) {
request.post({
url: 'http://teeSpring.com/login/submitLogin',
form: {
email: req.body.email,
password: req.body.password
}
}, function(error, response, body) {
if (error) {
console.log('Error is: '+JSON.stringify(error));
res.send(400, "There is some problem here.");
} else if(body){
console.log('Body is :')//+JSON.stringify(body));
var serverResp = analyzeData(body);
console.log('Server Response is :'+serverResp);
res.send(serverResp);
}
});
});
analyzeData = function(responseData) {
var respModel=[];
var errorMessage;
$ = cheerio.load(responseData);
$(".errors > p").each(function(i, element) {
console.log($(this).text().indexOf("The password you entered is incorrect"));
if ($(this).text().indexOf("The password you entered is incorrect") >= 0) {
console.log('finds the error')
var error = new Error("Password did not match");
error.code = 400;
errorMessage = error;
return false;
};
});
最佳答案
对于analyzeData()的成功案例,您没有返回任何内容
关于javascript - Node : Setting the error message and sending it back to UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134297/