简体   繁体   English

无法访问 angular 服务器响应中的 200 代码状态

[英]unable to access 200 code status in angular server response

Goal: If the server response with 200 code, I want to perform some changes on the record locally.目标:如果服务器响应 200 代码,我想在本地对记录执行一些更改。

issue: I am not able to access the response code or the 'message' attribute问题:我无法访问响应代码或“消息”属性

this is how the server response from this http call::这是服务器从这个 http 调用中响应的方式:

// MongooseModelSchema: this is the model that uses moongose 
    MongooseModelSchema.updateOne({ _id: req.params.id, creator: req.userData.userId }, channel)
      .then(result => {
        if (result.n > 0) {
          res.status(200).json({ message: "Update successful!" }); // this is what I want to capture
        } else {
          res.status(401).json({ message: "Not authorized!" });
        }
      })
      .catch(error => {
        console.log(error);
        res.status(500).json({
          message: "Couldn't udpate channel!"
        });
      });

I am able to hit the API no problem我可以打 API 没问题

I have the following http snip in my angular code::我的 angular 代码中有以下 http 片段::

    this.http
      .put(BACKEND_URL+'setState/' + id, channelData)
      .subscribe(response => {
        console.log('response',response);
        console.log('check if response has Update successful!: ',JSON.stringify(response).search('Update successful!'));
        console.log('response.message',response.message);

       this.router.navigate(["/editChannel", id]);
      })

this is the console.log image:这是 console.log 图像: 控制台日志图像

issue image: as you can see, i dont have access to the return code.问题图片:如您所见,我无权访问返回代码。 I also cant access the 'message' property UNTIL i run the code, then it works.... super weird.我也无法访问“消息”属性,直到我运行代码,然后它才能工作....超级奇怪。

消息属性错误

How do I check for the 200 code?如何检查 200 代码?

That makes sense.这就说得通了。 Message object is not typed, so compiler is telling you that message doesn't exist on response.消息 object 未键入,因此编译器告诉您该message在响应时不存在。 Instead what you should do is the following:相反,您应该做的是:

myresponse.ts:我的回应.ts:

interface MyResponse {
 message: string
}

this.http
      .put<MyResponse>(BACKEND_URL+'setState/' + id, channelData)
      .subscribe(response => {
        console.log('response',response);
        console.log('check if response has Update successful!: ',JSON.stringify(response).search('Update successful!'));
        console.log('response.message',response.message);

       this.router.navigate(["/editChannel", id]);
      })

now angular will grab the response and map it to the MyResponse interface giving you ability to access the message property.现在 angular 将获取响应,并将其 map 发送到MyResponse接口,使您能够访问message属性。 Alternatively you could keep it as any or unknown not sure what the type on response by default is, but if its any just do response['message']或者,您可以将其保留为anyunknown不确定默认情况下响应的类型是什么,但如果它any只是做response['message']

Hope that helps.希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM