【发布时间】:2018-12-27 06:24:34
【问题描述】:
如果插入查询成功,我想发送响应消息,否则 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'));
}
【问题讨论】:
-
将promise与then链一起使用,您可以发送成功和失败的响应。 成功 .then 和 catch 函数
-
你能解释一下吗
标签: mysql node.js express angular6