如果插入查询成功,我想发送响应消息,否则,nodejs应该将不成功的消息发送到angular 6,并在浏览器上显示该消息 // nodejs文件
exports.create = (req, res) => {
let uname = req.body.username;
let mail = req.body.email;
con.query('SELECT username FROM customers WHERE username = ?', uname,(err,result)=>{
if(err) throw err;
con.query('SELECT email FROM customers WHERE email = ?', mail,(err, results) => {
if(err) throw err;
if(result.length !=0 || results.length != 0){
console.log('email and username already exists');
res.send({msg:"already registered with the email and username"});
}
else{
con.query('INSERT INTO customers SET ?', req.body, (err, results) => {
if(err) throw err;
res.status(200).json(results);
});
}
});
});
};
//service.ts file
addCustomer (customer: Customer): Observable<Customer> {
return this.http.post<Customer>(this.customersUrl, customer, httpOptions);
}
//create component file
addCustomer() {
this.submitted = true;
this.save();
}
private save(): void {
this.customerService.addCustomer(this.customer)
.subscribe(result=>console.log('success'));
}
答案 0 :(得分:0)
使用then链编写api函数。您可以获取catch中的错误。返回成功函数和错误函数中需要的任何内容。 示例:
getQueryResults()
.then(processResultData)
.then((result)=>{
return result;
})
.catch((error)=>{
return error;
});