可以导出一个类,但不能导出一个函数在JS中。

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

Can export a class, but can not export a functions in JS

问题

I use different tech stacks for a current project and some things learn on the fly. I am not confident with different JS standards and their use cases. At this moment I have TypeError: chain.getSwapChainContractInstance is not a function which is a valid and exported function.

It is exported in a common for the project way via exports.getSwapChainContractInstance = function() { /* the rest of the code */}.

The module class is imported like this:

const chain = require('../models/chain/chain');
const { SwapToken, SwapChainV2 } = require('../models/chain/chain');

The whole code of the module is:

const { ethers } = require("ethers");
const config = require('config');

class Contract {
    constructor() {}

    getWalletWithProvider(privateKey, nodeUrl) {
        let provider = new ethers.providers.JsonRpcProvider(nodeUrl)
        let walletWithProvider = new ethers.Wallet(privateKey, provider);
        return walletWithProvider;
    }
}

class SwapToken extends Contract {

    constructor(privateKey, nodeUrl, contractAddress) {
        super();
        console.log(`${JSON.stringify(privateKey)}, ${JSON.stringify(nodeUrl)}, ${JSON.stringify(contractAddress)}`)
        let swapTokenAbiJson = [
            "function balanceOf(address owner) public view virtual override returns (uint256)",
        ];
        this.signerInstance = this.getWalletWithProvider(privateKey, nodeUrl);
        this.swapTokenContract = new ethers.Contract(
            contractAddress, 
            swapTokenAbiJson, 
            this.signerInstance
        );
    }

    async balanceOf(address) {
        return await this.swapTokenContract.balanceOf(address);
    }
}

class SwapChainV2 extends Contract {

    constructor(privateKey, nodeUrl, contractAddress) {
        super();
        let swapChainAbiJson = [
            "function registerUser(address user) public override",
            "function getUsers() public view override returns (address[] memory)",
            "function getMatches(address userFirst, address userSecond) external view override returns ((address,uint,address,uint,bool,bool)[])"
        ];
        this.signerInstance = this.getWalletWithProvider(privateKey, nodeUrl);
        this.swapChainContract = new ethers.Contract(
            contractAddress, 
            swapChainAbiJson, 
            this.signerInstance
        );
        
    }

    async getMatches(userFirstAddress, userSecondAddress) {
        return await this.swapChainContract.getMatches(userFirstAddress, userSecondAddress);
    }

    async getUsers() {
        return await this.swapChainContract.getUsers();
    }
}

/**
 * FIXME: by some reason I can not import it and use in the usual way 
 */
exports.getSwapChainContractInstance = function() {
    let nodeUrl = `http://${config.get("chain").host}:${config.get("chain").port}`;
    let privateKey = config.get("chain").privateKey;
    let swapChainAddress = config.get("chain").swapChainV2Address;

    return new SwapChainV2(privateKey, nodeUrl, swapChainAddress);
}

/**
 * FIXME: by some reason I can not import it and use in the usual way 
 */
exports.getSwapValueContractInstance = function() {
    let nodeUrl = `http://${config.get("chain").host}:${config.get("chain").port}`;
    let privateKey = config.get("chain").privateKey;
    let swapTokenAddress = config.get("chain").swapTokenAddress;

    return new SwapToken(privateKey, nodeUrl, swapTokenAddress);
}

module.exports = { SwapToken, SwapChainV2 }
英文:

I use different tech stacks for a current project and some things learn on the fly. I am not confident with different JS standards and their use cases. At this moment I have TypeError: chain.getSwapChainContractInstance is not a function which is a valid and exported function.

It is exported in a common for the project way via exports.getSwapChainContractInstance = function() { /* the rest of the code */}.

The module class is imported like this:

const chain = require('../models/chain/chain');
const { SwapToken, SwapChainV2 } = require('../models/chain/chain');

The whole code of the module is:

const { ethers } = require("ethers");
const config = require('config');

class Contract {
    constructor() {}

    getWalletWithProvider(privateKey, nodeUrl) {
        let provider = new ethers.providers.JsonRpcProvider(nodeUrl)
        let walletWithProvider = new ethers.Wallet(privateKey, provider);
        return walletWithProvider;
    }
}

class SwapToken extends Contract {

    constructor(privateKey, nodeUrl, contractAddress) {
        super();
        console.log(`${JSON.stringify(privateKey)}, ${JSON.stringify(nodeUrl)}, ${JSON.stringify(contractAddress)}`)
        let swapTokenAbiJson = [
            "function balanceOf(address owner) public view virtual override returns (uint256)",
        ];
        this.signerInstance = this.getWalletWithProvider(privateKey, nodeUrl);
        this.swapTokenContract = new ethers.Contract(
            contractAddress, 
            swapTokenAbiJson, 
            this.signerInstance
        );
    }

    async balanceOf(address) {
        return await this.swapTokenContract.balanceOf(address);
    }
}

class SwapChainV2 extends Contract {

    constructor(privateKey, nodeUrl, contractAddress) {
        super();
        let swapChainAbiJson = [
            "function registerUser(address user) public override",
            "function getUsers() public view override returns (address[] memory)",
            "function getMatches(address userFirst, address userSecond) external view override returns ((address,uint,address,uint,bool,bool)[])"
        ];
        this.signerInstance = this.getWalletWithProvider(privateKey, nodeUrl);
        this.swapChainContract = new ethers.Contract(
            contractAddress, 
            swapChainAbiJson, 
            this.signerInstance
        );
        
    }

    async getMatches(userFirstAddress, userSecondAddress) {
        return await this.swapChainContract.getMatches(userFirstAddress, userSecondAddress);
    }

    async getUsers() {
        return await this.swapChainContract.getUsers();
    }
}

/**
 * FIXME: by some reason I can not import it and use in the usual way 
 */
exports.getSwapChainContractInstance = function() {
    let nodeUrl = `http://${config.get("chain").host}:${config.get("chain").port}`;
    let privateKey = config.get("chain").privateKey;
    let swapChainAddress = config.get("chain").swapChainV2Address;

    return new SwapChainV2(privateKey, nodeUrl, swapChainAddress);
}

/**
 * FIXME: by some reason I can not import it and use in the usual way 
 */
exports.getSwapValueContractInstance = function() {
    let nodeUrl = `http://${config.get("chain").host}:${config.get("chain").port}`;
    let privateKey = config.get("chain").privateKey;
    let swapTokenAddress = config.get("chain").swapTokenAddress;

    return new SwapToken(privateKey, nodeUrl, swapTokenAddress);
}

module.exports = { SwapToken, SwapChainV2 }

答案1

得分: 2

在一个CommonJS模块中,exports的值是一个被导出的对象。默认情况下,它是一个空对象。

首先,你对这个对象进行了改变,添加了一个函数。

exports.getSwapChainContractInstance = function()

然后,你再次对它进行了修改,添加了第二个函数。

exports.getSwapValueContractInstance = function() {

然后,你用一个新的对象替换了整个对象,这个新对象导出了类。

module.exports = { SwapToken, SwapChainV2 }

你导出的新对象不包含你分配给旧对象的这两个函数。


选择一种方法来设置你的导出:

  • 修改对象四次,添加四个属性
  • 一次性用一个已经包含所有四个属性的新对象替换原对象
英文:

In a CommonJS module, the value of exports is an object that is exported. By default it is an empty object.

First you mutate the object to add a function to it.

> exports.getSwapChainContractInstance = function()

Then you mutate it again to add a second function to it.

> exports.getSwapValueContractInstance = function() {

Then you replace the whole object with a new one which exports the classes.

> module.exports = { SwapToken, SwapChainV2 }

The new object you are exporting doesn't have the two function you assigned to the old object.


Pick one method for setting your exports:

  • Mutate the object four times to add the four properties
  • Replace the object once with a single new object that already has all four properties

答案2

得分: -4

export function getSwapChainContractInstance() {
// your code
}

英文:
export function getSwapChainContractInstance() {
 // your code 
}

huangapple
  • 本文由 发表于 2023年2月27日 18:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579230.html
匿名

发表评论

匿名网友

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

确定