Mysql 如何使用angular和node在if条件下检查服务器响应

Mysql 如何使用angular和node在if条件下检查服务器响应,mysql,node.js,angular,Mysql,Node.js,Angular,我已经创建了一个带有angular和node的注册表。我想在数据插入数据库时显示sweetalert。我找不到检查服务器响应状态200的正确过程。请在下面找到我的代码 在form.component.ts中 onSubmit(data) { this.formDataservice.formData(data).subscribe( data => { if(data.status==200){ Swal.fire({

我已经创建了一个带有angular和node的注册表。我想在数据插入数据库时显示sweetalert。我找不到检查服务器响应状态200的正确过程。请在下面找到我的代码

在form.component.ts中

onSubmit(data)
  {
    this.formDataservice.formData(data).subscribe(
      data => {
        if(data.status==200){
        Swal.fire({
          icon: 'success',
          title: 'Registration Successful!',
        })
      }
    },
      error => console.log(error)
    );
  }
在职

url ="http://localhost:3000/insertion";

constructor(private http:HttpClient) { }

formData(data)
  {
    return this.http.post<any>(this.url,data, {observe: 'response'});
  }

您需要从服务器发送响应

app.post('/insertion',function(request, response){
    response.setHeader('Content-Type','application/json');
    
    var name = request.body.name;
    var mobile = request.body.mobile;
    var email = request.body.email;
    var office = request.body.office;

    var sql = "INSERT INTO `Angular` (`name`,`mobile`,`email`,`office`) VALUES ('"+name+"','"+mobile+"','"+email+"','"+office+"')";

    database.query(sql,function(err,res){
        if(err){
            console.log(err);
            response.status(500).json({
                           "error": "database error"
            })
        }else{
            console.log("Data inserted!");
            response.status(200).json({
                         "data" : "data inserted"
            })
        }
    });
});

目前,你的后端根本没有响应,事实上,它是一个角度应用程序,试图消费它是无关紧要的。您没有测试端点吗?但是你也不应该公开来自服务的全部响应,那么传输层的细节将在你的整个应用程序中传播。相反,想想什么对UI有用-也许可以公开一个布尔成功/失败的观察结果?谢谢。。。。。它起作用了
app.post('/insertion',function(request, response){
    response.setHeader('Content-Type','application/json');
    
    var name = request.body.name;
    var mobile = request.body.mobile;
    var email = request.body.email;
    var office = request.body.office;

    var sql = "INSERT INTO `Angular` (`name`,`mobile`,`email`,`office`) VALUES ('"+name+"','"+mobile+"','"+email+"','"+office+"')";

    database.query(sql,function(err,res){
        if(err){
            console.log(err);
            response.status(500).json({
                           "error": "database error"
            })
        }else{
            console.log("Data inserted!");
            response.status(200).json({
                         "data" : "data inserted"
            })
        }
    });
});