简体   繁体   English

Console.log 里面然后从 fetch 不工作

[英]Console.log inside then from fetch not working

I'm trying to debug the code from an expo app (react native) that posts a json to and endpoint.我正在尝试从 expo 应用程序(反应本机)调试代码,该应用程序将 json 发布到端点。 I've added console.log statements inside the then clause after the fetch but they are not printing at all: the other code is running fine :S我在 fetch 之后的 then 子句中添加了 console.log 语句,但它们根本没有打印:其他代码运行良好:S

  sendMessage: (
    token,
    data
  ) => {
    return new Promise((resolve, reject) => {
      const body = {
        ...data,
      };
      fetch(`${URI_BACKEND}api/create`, {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${token}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(body),
      })
      .then((response) => {
        console.log("RESPONSE FROM BACKEND", response);
        
        showToastGlobal("success", "top", "Saved", "Message saved successfully");
        return response.json();
      })
      .then(json => resolve(json))
      .catch(error => {console.log("ERROR FROM THE BACKEND", error); reject(error);});
    });
  },

The showToastGlobal function is being executed cause I can see the toast in app, any idea what am I doing wrong? showToastGlobal function 正在执行,因为我可以在应用程序中看到 toast,知道我做错了什么吗? no error log and no response log...没有错误日志,也没有响应日志...

UPDATE: added caller function更新:添加调用者 function

const sendWholeMessage = (messageId, messageToSend, images, timestamp, userId) => {
    API.sendMessage(token, messageToSend)
    .then(response => {
      if (images[0]) {
        console.log("Sending images...");
        API.saveImages(token, images)
        .then(result => console.log("Response img:", result))
        .catch(error => console.log("ERROR sending images to backend", error));
      }
      console.log("Response Message:", response);
      ...
      deleteLocalCopyOfMessage(messageId);
      console.log("Saving...")
    });
  }

The "Response Message" and the "Saving..." console logs both print ok “响应消息”和“正在保存...”控制台日志都打印正常

UPDATE 2: changing更新2:改变

        console.log("RESPONSE FROM BACKEND", response);

to

        console.log("RESPONSE FROM BACKEND", response.status);

does print to console, or removing the second argument from console.log (just the string) does also work, so trying to print the response object results in the console.log statement being ignored, any ideas as to why this happens?打印到控制台,或者从 console.log 中删除第二个参数(只是字符串)也可以,所以尝试打印响应 object 会导致 console.log 语句被忽略,关于为什么会发生这种情况的任何想法?

Problem问题

does not receive response using fetch API.使用 fetch API 未收到响应。

Solution解决方案

fetch API need two steps to get the response, so I would suggest you to retrieve response in the second then() clause: fetch API 需要两个步骤来获取响应,所以我建议您在第二个 then() 子句中检索响应:

  fetch(`${URI_BACKEND}api/create`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(body),
  })
  .then((response) => {
    return response.json();
  })
  .then(json =>         
    console.log("RESPONSE FROM BACKEND", json);        
    showToastGlobal("success", "top", "Saved", "Message saved successfully");)
  .catch(error => {console.log("ERROR FROM THE BACKEND", error); reject(error);});

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

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