Ionic 参考错误:调用以太坊合约方法时"originalStackTrace未定义"

tzdcorbm  于 2022-12-31  发布在  Ionic
关注(0)|答案(1)|浏览(108)

我正在尝试使用以太坊和web3构建一个Ionic dapp,到目前为止,我已经成功地创建了一个契约示例,但似乎当我试图调用一个方法来获取契约中的信息时,我得到了以下错误:

ReferenceError: originalStackTrace is not defined
    at new PromiEvent (:8100/webpack:/node_modules/@truffle/contract/lib/promievent.js:8)
    at Function.call (:8100/webpack:/node_modules/@truffle/contract/lib/execute.js:121)
    at AppComponent.<anonymous> (:8100/webpack:/src/app/app.component.ts:96)
    at Generator.next (<anonymous>)
    at fulfilled (:8100/webpack:/node_modules/tslib/tslib.es6.js:71)
    at ZoneDelegate.invoke (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:364)
    at Object.onInvoke (:8100/webpack:/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:41654)
    at ZoneDelegate.invoke (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:363)
    at Zone.run (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:123)
    at :8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:857
    at ZoneDelegate.invokeTask (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:399)
    at Object.onInvokeTask (:8100/webpack:/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:41632)
    at ZoneDelegate.invokeTask (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:398)
    at Zone.runTask (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:167)
    at drainMicroTaskQueue (:8100/webpack:/node_modules/zone.js/dist/zone-evergreen.js:569)

下面是组件中的代码:

testContractCall() {
    this.w3.artifactsToContract(metacoin_artifacts)
    .then(async (MetaCoinAbstraction) => {
      this.TestContract = TestContractAbstraction;
      try{
        const deployedContract = await this.TestContract.deployed();
        console.log("DEPLOYED: ", deployedContract); //it does print it, and it has the method
        
        const result = await deployedContract.getTestName.call();
        console.log("Le result: ", result); //it doesn't reach here, goes to catch

      } catch(e) {
        console.error("Error: ",e);
      } 
    });
  }

合同是这样的:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.21 <0.7.0;

contract TestContract {

    struct TestUser {
        string name;
        uint age;
        uint id;
    }

    uint lastUser;

    mapping(uint => TestUser) usuarios;

    constructor () public {
        usuarios[1] = TestUser({name: "testName", age: 21, id: 1});
        lastUser = 1;
    }

    function getTestName() public view returns(string memory) {
        return usuarios[1].name;
    }

}

另外,下面是我的节点依赖关系:

"dependencies": {
    "@angular/common": "~9.1.6",
    "@angular/core": "~9.1.6",
    "@angular/forms": "~9.1.6",
    "@angular/platform-browser": "~9.1.6",
    "@angular/platform-browser-dynamic": "~9.1.6",
    "@angular/router": "~9.1.6",
    "@ionic-native/core": "^5.27.0",
    "@ionic-native/splash-screen": "^5.27.0",
    "@ionic-native/status-bar": "^5.27.0",
    "@ionic/angular": "^5.2.3",
    "@truffle/contract": "^4.2.11",
    "cordova-android": "^8.1.0",
    "fs": "0.0.1-security",
    "rxjs": "~6.5.1",
    "truffle-contract": "^4.0.31",
    "tslib": "^1.10.0",
    "web3": "^1.2.9",
    "zone.js": "~0.10.2"
  }
46scxncf

46scxncf1#

错误在@truffle/contract库中。我用truffle-contract解决了它。对于任何最终遇到这种情况的人:

npm uninstall @truffle/contract
npm i truffle-contract

以后每隔

require('@truffle/contract')

require('truffle-contract')

就我而言,它们的使用方式是相同的,所以代码应该工作得很好。

相关问题