簡體   English   中英

發布到服務器后如何控制台日志或警告數據

[英]how to console log or alert the data after posted in the server

我正在我的服務器上發布反饋數據,並希望在警報中查看發布的數據。

export const postFeedback = (
  firstname,
  lastname,
  telnum,
  email,
  agree,
  contactType,
  message
) => (dispatch) => {
  const newFeedback = {
    firstname: firstname,
    lastname: lastname,
    telnum: telnum,
    email: email,
    agree: agree,
    contactType: contactType,
    message: message,
  };
  newFeedback.date = new Date().toISOString();

  return fetch(baseUrl + 'feedback', {
    method: 'POST',
    body: JSON.stringify(newFeedback),
    headers: {
      'Content-Type': 'application/json',
    },
    credentials: 'same-origin',
  })
    .then(
      (response) => {
        if (response.ok) {
          return response;
        } else {
          var error = new Error(
            'Error ' + response.status + ': ' + response.statusText
          );
          error.response = response;
          throw error;
        }
      },
      (error) => {
        throw error;
      }
    )
    .then((response) => response.json())
    .then((response) => dispatch(addFeedback(response)))
    .then((response) => alert(response))
    .catch((error) => {
      console.log('post feedback', error.message);
      alert('Your feedback could not be posted\nError: ' + error.message);
    });
};

數據已正確發布,但在警報中我只看到[object Object]為什么? 發布到服務器后如何控制台日志或警告數據?

圖片

如果有人幫助我解決這個問題,我衷心感謝。

您可以使用JSON.stringify(obj)記錄錯誤,因為alert僅顯示文本

您需要將 object 解析為字符串,使用JSON.stringify(x)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

[object Object]是 object 的字符串化表示。

任何一個
alert(JSON.stringify(error.message))

或者

console.log(error.message)

嘗試將 JSON 轉換為字符串,你可以試試這個。

export const postFeedback = (
  firstname,
  lastname,
  telnum,
  email,
  agree,
  contactType,
  message
) => (dispatch) => {
  const newFeedback = {
    firstname: firstname,
    lastname: lastname,
    telnum: telnum,
    email: email,
    agree: agree,
    contactType: contactType,
    message: message,
  };
  newFeedback.date = new Date().toISOString();

  return fetch(baseUrl + 'feedback', {
    method: 'POST',
    body: JSON.stringify(newFeedback),
    headers: {
      'Content-Type': 'application/json',
    },
    credentials: 'same-origin',
  })
    .then(
      (response) => {
        if (response.ok) {
          return response;
        } else {
          var error = new Error(
            'Error ' + response.status + ': ' + response.statusText
          );
          error.response = response;
          throw error;
        }
      },
      (error) => {
        throw error;
      }
    )
    .then((response) => response.json())
    .then((response) => dispatch(addFeedback(response)))
    .then((response) => alert(JSON.stringify(response)))
    .catch((error) => {
      console.log('post feedback', error.message);
      alert('Your feedback could not be posted\nError: ' + error.message);
    });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM