【问题标题】:React: This Request has No Response Data AvailableReact:此请求没有可用的响应数据
【发布时间】:2022-12-07 01:53:03
【问题描述】:

我在 React Typescript 中发出了 API 请求。在代码中,没有数据,因此 API 返回以下内容:this request has no response data available

当我尝试验证响应时,在下面的最后一行代码中,它会打印空的支付响应,但不会输入 if 语句,控制台日志语句为loading error

为什么不进入if语句?我需要在 if 语句中运行一些代码。

应用程序接口

export const getPaymentsById = (
  paymentId: number,
): Promise<PaymentByIdResponse> =>
  kfetch(`/billing-payments-ui/api/payments/${paymentId}`);

export type PaymentByIdResponse = {
  paymentId?: number;
  paymentAmount?: number;
  postingDate?: string;
  paymentMethod?: string;
  confirmationNumber?: string;
  paymentDescription?: string;
};

const paymentResponse = await getPaymentsById(paymentIdNumber);
console.log('paymentResponse', paymentResponse);
if (paymentResponse == null) {
  console.log('loading error');
  setIsLoadingError(true);
  ....
}

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    看起来 if 语句中的条件未评估为真。这可能是因为您使用的是比较运算符 (==) 而不是严格相等运算符 (===)。比较运算符将检查两个变量的值是否相等,但会在检查之前尝试将它们转换为通用类型。严格相等运算符将检查两个变量是否属于同一类型并具有相同的值。尝试将 if 语句更改为:

    if (paymentResponse === null) {
      console.log('loading error');
      setIsLoadingError(true);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2016-03-30
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多