我想在 get 方法中使用 Promise 返回值。 Node.js 休息 API

发布于 2025-01-10 20:07:37 字数 690 浏览 0 评论 0原文

我正在创建一个休息 api。我的 get 方法将根据合约的总供应值返回结果,否则不会响应,但我对合约发出的请求返回一个承诺。我该如何使用这个值?

const NameContract = new web3.eth.Contract(abi, '0xE3A2beCa..........1D901F8');
NameContract.methods.totalSupply().call().then(value => console.log(value))


app.get('/:id', (req, res) => {
    let id = parseInt(req.params.id);
    //I want to use an if here. 
    //I want to throw the query according to the value returned from above,
    // but it returns a promise, how can I use it value?
    nft.findOne({ id: id }, (err, doc) => {
        if (doc != null) {
            res.json(doc)
        }
        else {
            res.status(404).json(err)
        }
    });

});

I am creating a rest api. My get method will return the result according to the total supply value of the contract or it will not respond, but the request I made to the contract returns a promise. How can I use this value?

const NameContract = new web3.eth.Contract(abi, '0xE3A2beCa..........1D901F8');
NameContract.methods.totalSupply().call().then(value => console.log(value))


app.get('/:id', (req, res) => {
    let id = parseInt(req.params.id);
    //I want to use an if here. 
    //I want to throw the query according to the value returned from above,
    // but it returns a promise, how can I use it value?
    nft.findOne({ id: id }, (err, doc) => {
        if (doc != null) {
            res.json(doc)
        }
        else {
            res.status(404).json(err)
        }
    });

});

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

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

发布评论

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

评论(1

热鲨 2025-01-17 20:07:37

尝试保存其值最终将成为总值的承诺

 const pTotalSupply = NameContract.methods.totalSupply().call();

(假设这是有效的 - .call 方法看起来有点奇怪)。然后,选项是

  • 在满足上述 pTotalSupply 之前不启动服务器,提供总供应量是一个静态值,并且服务器必须处理一个名称合同,
  • 等待在 app.get 处理程序中获取结果,方法是在异步函数体内使用 await 运算符,或者将请求处理包装在 Promise >then 参数函数,或
  • 使用一个承诺 then 调用来保存供应值并在两部分 app.get 处理程序中调用 next。这更像是一种面向表达的解决方案,使用以下模式:
app.get('/:id', (req, res, next) => {
    pTotalSupply.then( value => {
        res.locals.value = value;
        next();
    }
    .catch( err => res.status(500).send("server error")); // or whatever
 }
 , (req, res) => {
    const value = res.locals.value;
    let id = parseInt(req.params.id);
    // process request with value and id:
    ...
 });

第二个选项包含在 如何从异步调用返回响应

Try saving the promise whose value will end up being the total value

 const pTotalSupply = NameContract.methods.totalSupply().call();

(assuming this is valid - the .call method looks a little strange). Options then are to

  • Not start the server until pTotalSupply above has been fulfilled, provided total supply is a static value and there to be only one name contract the server has to deal with,
  • Wait for the result to be obtained within the app.get handler, either by using an await operator inside an asynchronous function body, or wrapping request processing in a promise then argument function, or
  • Using a promise then call to save the supply value and call next in a two part app.get handler. This is more an express oriented solution using a pattern along the lines of:
app.get('/:id', (req, res, next) => {
    pTotalSupply.then( value => {
        res.locals.value = value;
        next();
    }
    .catch( err => res.status(500).send("server error")); // or whatever
 }
 , (req, res) => {
    const value = res.locals.value;
    let id = parseInt(req.params.id);
    // process request with value and id:
    ...
 });

The second option is covered in answers to How to return the response from an asynchronous call

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