当我们没有价值返回特定Id Angular6时如何处理错误

时间:2018-09-19 15:55:06

标签: node.js angular6

我是这个API和Angular World的新手。所以我需要一些澄清的人。在这里,我使用州ID获取地区数据。 只需检查我的Angular NodeJS代码并指导我如何解决此类错误

我的addstudent.ts文件

getSections(id: number) {
    let classdata:any = {};
    classdata.classid = id;
    this.globalmethods.getSections(classdata).subscribe( (data) => {
      this.sections = data.Active;
    }, (error) => {
      console.log('Error');
    })
  }

我的获取地区数据服务

getDistricts(stateid: number) {
    return this.http.post(this.globalurl+'districts', stateid, this.getOptions()).map((response: Response) => {
      return response.json();
    })
  }

我在Node.js中的后端代码

router.post('/districts', (req, res) => {
    const stateid = req.body.stateid;
    db.query("select id, name from lkup_district where stateid = '"+stateid+"' ", (error, rows) => {
        if(error) {
            console.log(error);
            res.status(500).send("Error with the query");
        }
        if(rows.length>0) {
            res.send(rows);
        }
        else {
            res.status(400).send({"error":"No data found"});
        }
    });
});

当我选择没有地区的状态时控制台出现错误

POST http://localhost:3000/api/global/districts 400 (Bad Request)
core.js:1673 ERROR Response {_body: "{"error":"No data found"}", status: 400, ok: false, statusText: "Bad Request", headers: Headers, …}

请指导我在哪里更改代码。

在此先谢谢您的帮助。

0 个答案:

没有答案