节点服务器正在发布到第三方网站并获得响应。
如果响应包含Password didn't match.
之类的内容,服务器将向UI发送错误,如400: Bad password.
。否则,服务器将分析响应并从中发送部分数据。
我无法将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;
};
});
答案 0 :(得分:0)
您在analyzeData()
上没有为成功案例返回任何内容