【问题标题】:How to migrate request-promise to axios or fetch如何将 request-promise 迁移到 axios 或 fetch
【发布时间】:2021-12-17 01:08:07
【问题描述】:

我想使用 React-Native 编写一个应用程序,该应用程序从具有 cookie 身份验证的网站加载 JSON 文件。 为了测试,我在没有 React-native 和 request-promise 的普通 JS 文件中尝试了它。

const fs = require("fs");
const request = require("request-promise").defaults({ jar: true });

async function main() {
  var incodeHeader = "";
  var incodeToken = "";

  try {
    const loginResult = await request.post("https://somepage/login.php", {
      form: {
        client: "XXX",
        login: "username",
        password: "password",
      },
    });
  } catch (err) {
    console.log(err);
  }

  incodeHeader = getIncodeHeader();
  incodeToken = getIncodeToken();

  const data = await request.post("https://somepage/load.json", {
    headers: {
      [incodeHeader]: incodeToken,
    },

    form: {
      max: "10",
    },
  });

  fs.writeFileSync("data.json", data);
}

main();

效果很好,所以我想在我的 App 中使用这种方法,但是我找不到在 React-Native 中使用 request-promise 的方法,所以我决定使用 axios。

const axios = require("axios");
const qs = require("qs");
axios.defaults.withCredentials = true;

async function main() {
  const data = {
    client: "XXX",
    login: "username",
    password: "password",
  };

  await axios
    .post("https://somepage/login.php", qs.stringify(data))
    .catch((err) => console.log(err));

  const incodeHeader = getIncodeHeader();
  const incodeToken = getIncodetoken();

  await axios
    .get(
      "https://somepage/load.json",
      { data: { max: "5" } },
      {
        headers: {
          [incodeHeader]: incodeToken,
        },
      }
    )
    .then((respone) => console.log(respone))
    .catch((err) => console.log(err));
}

main();

但在这段代码中,甚至登录都不起作用,我真的不知道为什么。有人可以告诉我如何正确地做到这一点,或者可以告诉我另一种适用于 React-Native 的解决方案吗?

【问题讨论】:

  • 附带说明,您可能希望自己熟悉 SOLID 原则。
  • 如果您使用await,请删除所有.then()。使用旧的类似 Promise 的语法 (.then()) 或新的 async/await,不要同时使用。另外,请define "it's not working"?您是否尝试过console.log() 的东西,看看您的函数是否被调用以及数据是否正确?控制台中的任何错误?等等。
  • 我认为您不需要qs.stringify()。我不太确定[incodeHeader]: incodeToken, 部分。请再次阅读the docs
  • 哈,以前你打电话给request.post,现在你打电话给axios.get。它应该是axios.post。您不能使用 GET 发送数据对象。

标签: javascript node.js react-native axios xmlhttprequest


【解决方案1】:

await axios.get 更改为await axios.post

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
  • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
【解决方案2】:

首先,我不知道您为什么要在第一个请求中对请求正文进行字符串化,axios 已经处理了这个,您可以只传递 data 对象,也许它是您问题的解决方案。

第二个(只是一个提示)。创建一个帮助器对象来发出 http 请求并且不直接实例化 axios,因此,您可以以一种简单的方式更改 http 请求处理程序,而不是在每个文件上更改它,有一天您可能需要这样做,如果您想保留您的应用已更新。

第三,不要混用awaitthen,选择:

try {
  const result = await action();

  // ...
} catch (err) {
  // ...
}

action()
  .then((result) => {
    // ...
  })
  .catch((err) => {
    // ...
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 2023-02-07
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多