【问题标题】:Get/catch response status in a promise在 Promise 中获取/捕获响应状态
【发布时间】:2019-08-14 17:08:29
【问题描述】:

我有一个承诺,它有时会给出错误(400 和 403,取决于用户)响应,取决于用户)。我想尝试捕捉响应,做一个条件(如果),这样我就可以根据服务器的响应,做一个不同的功能。

看起来像:

let item = pnp.sp.web.lists
   .getById(Constants.eventCalendarGuid)
   .items
   .getById(this.props.item.ID);

item.attachmentFiles.add("file2.txt", "Here is my content")
   .then(v => console.log(v));

我尝试了 then 链和 ==> 捕获 => 错误,但根本无法获得响应代码。

【问题讨论】:

  • 你能在服务器中显示代码吗?

标签: javascript reactjs typescript sharepoint


【解决方案1】:

示例演示:

item.attachmentFiles.add("file2.txt", "Here is my content")
   .then(v => console.log(v))
   .catch((err) =>{
     if(err.status==400)
      console.log(err.status);
    else
      console.log(err.status);
   });

【讨论】:

    【解决方案2】:

    你只需要使用catch函数

    item.attachmentFiles.add("file2.txt", "Here is my content")
      .then(v => console.log(v))
      .catch(err => console.log(err));
    

    【讨论】:

    • 我确实尝试过,但我收到的是完整消息,而不是状态码。 IE。不能在条件中使用。
    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2017-12-13
    • 2016-02-07
    • 2017-09-26
    相关资源
    最近更新 更多