从django-rest-api中获取with react Frontend的JSON数据成变量

发布于 2025-02-10 11:55:20 字数 1393 浏览 1 评论 0 原文

我是React和JavaScript的新手。我试图将一些数据 React>中的变量中获取,来自 django-rest api。

我使用React-Beautiful-DND在另一个函数中使用此变量。

我设置了API,并且还设置了CORS。

这是

我试图将其作为React Frontend中的JSON对象。

const getData = async () => {
  let response = await fetch("http://127.0.0.1:8000/api/column_names/");

  let data = await response.json();
  console.log(data);  // line 29

  return data;
};
let columnFromBackend = getData();
console.log(columnFromBackend);  // line 34

这是输出

“在此处输入图像描述”

是否有任何方法可以将此数据获取到变量 columnfrombackend

我试图获得该值的等效词。

let columnFromBackend = {
  idone: {
    name: "In progress",
    items: [{ id: "hjnmkjnh", content: "first" }],
  },
  idtwo: {
    name: "Todod",
    items: [],
  },
};

如果我直接声明变量,那么这就是我要获得的输出以及我试图从API中获得的内容。

I am new to react and javascript. I am trying to get some data INTO A VARIABLE in react from django-rest API.

I am using this variable in another function using react-beautiful-dnd.

I have the API set up and CORS also set up.

This is the output in http://127.0.0.1:8000/api/column_names/

I am trying to get this as a json object in react frontend.

const getData = async () => {
  let response = await fetch("http://127.0.0.1:8000/api/column_names/");

  let data = await response.json();
  console.log(data);  // line 29

  return data;
};
let columnFromBackend = getData();
console.log(columnFromBackend);  // line 34

This is the output

enter image description here

Is there any way to get this data into the variable columnFromBackend

I am trying to get the equivalent of this value.

let columnFromBackend = {
  idone: {
    name: "In progress",
    items: [{ id: "hjnmkjnh", content: "first" }],
  },
  idtwo: {
    name: "Todod",
    items: [],
  },
};

If I declare the variable directly like this, then this is the output that I am getting and what I am trying to get from the api.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2025-02-17 11:55:20

您需要将第二行更改为

let columnFromBackend = await getData();

您正在使用哪个版本的节点?

You need to change your second to last line to

let columnFromBackend = await getData();

Which version of node are you using?

哎呦我呸! 2025-02-17 11:55:20
  1. 转到 node_modules/react-scripts/config 和Open webpack.config.js 文件
    然后查找 module.exports 并添加

    实验:{toplevelawait:true}

  2. 然后 代码>让columnfrombackend =等待getData();

  1. Go to node_modules/react-scripts/config and open webpack.config.js file
    then look for module.exports and add

    experiments:{ topLevelAwait: true }

  2. Then change let columnFromBackend = getData(); to let columnFromBackend = await getData();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文