首页 > 解决方案 > TronWeb UnhandledPromiseRejectionWarning:TypeError:e.forEach 不是函数

问题描述

我正在尝试构建与前端的简单 tron 智能合约的接口,但我不能!这是问题:

PS D:\trontest> node index.js
(node:13816) UnhandledPromiseRejectionWarning: TypeError: contract is not a function
    at f (D:\trontest\index.js:45:26)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13816) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13816) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

索引.js

const TronGrid = require("trongrid");
const TronWeb = require("tronweb");

const tronWeb = new TronWeb({
    fullHost: 'https://api.shasta.trongrid.io'
});


const contractAddress = "TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ";
async function f() {
    let contract = await tronWeb.contract().at(contractAddress);
    const result = await contract().f().call();
    console.log(result);
}
f();

constract 有两个方法 f() 和 g() 返回字符串,但我不能调用它

包.json

{
  "name": "trontest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "trongrid": "^1.2.6",
    "tronweb": "^3.2.6"
  }
}

UPD1 进行 一些更改

const TronGrid = require("trongrid");
const TronWeb = require("tronweb");

const tronWeb = new TronWeb({
    fullHost: 'https://api.shasta.trongrid.io',
    headers: { "TRON-PRO-API-KEY": '*****-e677-4f4c-a9cf-2156aa5e8453' },
    privateKey: 'c4f27f7b052350703bcf*****6ecddfdd612b21c891'
});

async function f() {
    let instance = await tronWeb.contract("TNSRwKB4YpLQTbmG9tST9zAqXXssho5hgZ");
    let result = await instance().f().call();
    console.log(result);
}
f();
D:\trontest>node index.js
(node:1360) UnhandledPromiseRejectionWarning: TypeError: e.forEach is not a function
    at e.value (D:\trontest\node_modules\tronweb\dist\TronWeb.node.js:1:110856)
    at new e (D:\trontest\node_modules\tronweb\dist\TronWeb.node.js:1:108814)
    at s.value (D:\trontest\node_modules\tronweb\dist\TronWeb.node.js:1:147871)
    at f (D:\trontest\index.js:44:34)
    at Object.<anonymous> (D:\trontest\index.js:48:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)

现在我看到问题是 UnhandledPromiseRejectionWarning: TypeError: e.forEach is not a function

标签: javascriptnode.jsblockchainsolidity

解决方案


尝试

实例 = 等待 tronWeb.contract().at(trc20ContractAddress)


推荐阅读