express and moralis token price API not fetching json data in console

huangapple go评论62阅读模式
英文:

express and moralis token price API not fetching json data in console

问题

I've been following video link from Moralis web3 (youtube) meanwgile I got stuck when I need to fetch data using token price Moralis API. I want the price details to be printed in the console when I do
npm start
the expected output in console is:

{
  nativePrice: {
    value: '13851123944545175839',
    decimals: 18,
    name: 'Ether',
    symbol: 'ETH'
  },
  usdPrice: 23176.58785953117,
  exchangeAddress: '0x1f98431c8ad98523631ae4a59f267346ea31f984',
  exchangeName: 'Uniswap v3'
}

In localhost, it should return an empty JSON object '{}', but when I opened the same in localhost, it is showing:

Cannot GET /tokenPrice

I tried different methods provided in Moralis doc, it's working fine, but I did the same as the tutorial, and that throws me an error:

const express = require("express");
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");
const app = express();
const cors = require("cors");
require("dotenv").config();
const port = 3001;

app.use(cors());
app.use(express.json());

app.get("./tokenPrice", async (req, res) => {
  const { query } = req;

  const responseOne = await Moralis.EvmApi.token.getTokenPrice({
    address: query.addressOne,
  });

  const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
    address: query.addressTwo,
  });

  console.log(responseOne.raw);
  console.log(responseTwo.raw);

  return res.status(200).json({});
});

Moralis.start({
  apiKey: process.env.MORALIS_KEY,
}).then(() => {
  app.listen(port, () => {
    console.log(`Listening for API Calls`);
  });
});

Also, I want to know what {query} & addressOne means here as I've never declared any variable like that before in my code.

  • I want to know what {query} and addressOne are, whether Express.js properties or Moralis.
  • I want to know why and where the error occurred and a solution to resolve it.
英文:

I've been following video link from Moralis web3 (youtube) meanwgile I got stuck when I need to fetch data using token price Moralis API. I want the price details to be printed in the console when i do

npm start

the expected output in console is:

{
  nativePrice: {
    value: '13851123944545175839',
    decimals: 18,
    name: 'Ether',
    symbol: 'ETH'
  },
  usdPrice: 23176.58785953117,
  exchangeAddress: '0x1f98431c8ad98523631ae4a59f267346ea31f984',
  exchangeName: 'Uniswap v3'
}

In localhost it should return empty json object '{}' but when I opened the same in localhost it is showing:

Cannot GET /tokenPrice

I tried different method provided in moralis doc its working fine but I did same as the tutorial that throws me error:

const express = require("express");
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");
const app = express();
const cors = require("cors");
require("dotenv").config();
const port = 3001;

app.use(cors());
app.use(express.json());

app.get("./tokenPrice", async (req, res) => {
  const { query } = req;

  const responseOne = await Moralis.EvmApi.token.getTokenPrice({
    address: query.addressOne,
  });

  const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
    address: query.addressTwo,
  });

  console.log(responseOne.raw);
  console.log(responseTwo.raw);

  return res.status(200).json({});
});

Moralis.start({
  apiKey: process.env.MORALIS_KEY,
}).then(() => {
  app.listen(port, () => {
    console.log(`Listening for API Calls`);
  });
});

also I want to know what does that {query} & addressOne means here as I've never declared any var like before in my code.

  • I want to know what {query} and addressOne is , whether express js property or moralis
  • want to know why and where error occured and solution to resolve.

答案1

得分: 0

关于addressOneaddressTwo,这些是您请求中使用的查询参数。如果您观看视频,您可以看到他展示了请求应该是什么样子的示例:

http://localhost:3001/tokenPrice?addressOne=0x51491...cf986ca&addressTwo=0xdac17f...6994597

并使用两个代币的地址来获取价格。您确实可以扩展逻辑以使用更多地址。

在视频的45:00分钟处,您可以看到代码应该是什么样子的。请确保完善您的代码以使其正常工作。

express and moralis token price API not fetching json data in console

英文:

For addressOne and addressTwo, those are the query parameters used in your request. If you check the video you can see him showcasing an example of how the request should look like:

http://localhost:3001/tokenPrice?addressOne=0x51491...cf986ca&addressTwo=0xdac17f...6994597

And use 2 addresses of the tokens you wish to get the price for. You can indeed extend the logic to use more addresses

In the video at minute 45:00 You can see how the code should look like. Please make sure you complete your code for it to work properly.

express and moralis token price API not fetching json data in console

huangapple
  • 本文由 发表于 2023年2月8日 19:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定