gpt4 book ai didi

mysql - 我想从 nodejs 获得对 angular 6 的响应

转载 作者:行者123 更新时间:2023-11-30 21:35:44 26 4
gpt4 key购买 nike

如果插入查询成功,我想发送响应消息,否则 nodejs 应该向 angular 6 发送不成功的消息并在浏览器上显示 //Node 文件

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'));
}

最佳答案

使用 then 链编写 api 函数。您可以在 catch 中获取错误。在成功函数和错误函数中返回你需要的任何东西。示例:

 getQueryResults()
.then(processResultData)
.then((result)=>{
return result;
})
.catch((error)=>{
return error;
});

关于mysql - 我想从 nodejs 获得对 angular 6 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53940634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com