如何在记录之前等待json返回值(返回至少需要30秒)? javascript/react/express [英] How to await a json return value (the return takes at least 30 seconds) before logging it? javascript/react/express

查看:92
本文介绍了如何在记录之前等待json返回值(返回至少需要30秒)? javascript/react/express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这些线程的后续内容:

This is a follow up to these threads:

如何将json数据返回到反应状态?

我正在使用web3Ethereum上签署交易,然后发送带有交易数据的json对象. json至少需要30秒才能返回.

I am using web3 to sign a transaction on Ethereum, which then sends a json object with transaction data. The json takes at least 30 seconds to return.

我正在尝试使用以下代码console.log()数据:

I am trying to console.log() the data with the following code:

axios.post(
    "http://compute.amazonaws.com:3000/users", 
    {
        value: "value",
        fileName: "fileName",
        hash: "hash"
    }
)
.then(res => { console.log(res.data);});

控制台中没有日志.

以上实际上是一个更大函数的一部分,我已经将其制成了async并添加了await.这不会产生任何结果或错误:

The above is actually part of a bigger function, which I have made async and added await. This did not produce any results or errors:

const onSuccess = async payment => {
      axios.post(
        "http://ec2-54-67-28-69.us-west-1.compute.amazonaws.com:3000/users",
        {
        value: "value",
        fileName: "fileName",
        hash: "hash"
        }
      );
      await (res => {
        console.log(res.data);
      });

我认为问题是Ethereum需要一些时间来挖掘交易.我需要等待json响应,然后再记录它.

I think the issue is that Ethereum takes some time to mine the transaction. I need to wait for the json response before logging it.

我的express服务器没有问题,json已成功登录到服务器.

My express server is having no issues, the json is successfully logged on the server.

有什么想法吗?谢谢!

推荐答案

第一段代码似乎可以正常工作,可能是您想将.catch作为错误处理程序附加到该代码上.

First block of code seems working, may be you want to attach .catch to it as error handler.

axios.get('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => console.log(response.data))
  .catch(function(error) {
    console.log(error);
  });

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

在第二部分中,我认为您做错了await.您正在等待一个永远不会被调用的函数.在下面的实例中,等待应该被监听在axios

In second part I think you are doing await wrong. You are awaiting on a function which never gets called. Await is supposed to be listening on something that gets called in below instance a promise returned by axios

const samplePayload = {
  "userId": 100,
  "id": 100,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
};


// Using GET here for demo but you can swap it with POST
const onSuccess = async payment => {
  return await axios.get(
    "https://jsonplaceholder.typicode.com/posts", samplePayload
  ).then(res => {
    console.log(res.data);
  });
}

console.log(onSuccess());

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

我希望能弄清楚.

这篇关于如何在记录之前等待json返回值(返回至少需要30秒)? javascript/react/express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆