在调用chaincode时出现错误,因为“参数数目错误。应为6”

时间:2019-08-06 04:43:11

标签: hyperledger-fabric hyperledger

我正在尝试调用chaincode函数,但是出现错误“参数数目不正确。应为6”。

我已经检查我的chainocode了,它运行良好。我不明白为什么它给错误。因为函数中的参数个数正确。

Chaincode函数

func (s *SmartContract) recordProduce(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

    if len(args) != 5 {
        return shim.Error("Incorrect number of arguments. Expecting 5")
    }

    var Produce = Produce{ProduceName: args[1], Health: args[2], Owner : arg[3], FarmID: args[4]}

    ProduceAsBytes, _ := json.Marshal(Produce)
    APIstub.PutState(args[0], ProduceAsBytes)

    return shim.Success(nil)
}

Invoke.js

'use strict';

const { FileSystemWallet, Gateway } = require('fabric-network');
const path = require('path');

const ccpPath =  path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json');

async function main() {
    try {

        // Create a new file system based wallet for managing identities.
        const walletPath = path.join(process.cwd(), 'wallet');
        const wallet = new FileSystemWallet(walletPath);
        console.log(`Wallet path: ${walletPath}`);

        // Check to see if we've already enrolled the user.
        const userExists = await wallet.exists('user1');
        if (!userExists) {
            console.log('An identity for the user "user1" does not exist in the wallet');
            console.log('Run the registerUser.js application before retrying');
            return;
        }

        // Create a new gateway for connecting to our peer node.
        const gateway = new Gateway();
        await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: false, asLocalhost: true } , eventHandlerOptions: {
            strategy: null
        } 
    });

        // Get the network (channel) our contract is deployed to.
        const network = await gateway.getNetwork('dfarmchannel');

        // Get the contract from the network.
        const contract = network.getContract('produce-app');


        await contract.submitTransaction('recordProduce', 'PR12', 'Banana', 'Good', 'Abhi', 'FARM111');
        console.log('Transaction has been submitted');

        // Disconnect from the gateway.
        await gateway.disconnect();

    } catch (error) {
        console.error(`Failed to submit transaction: ${error}`);
        process.exit(1);
    }
}

main();

错误: enter image description here

1 个答案:

答案 0 :(得分:1)

您的源代码显示错误消息“参数数不正确。期望5”,但是您得到了“参数数不正确。期望6”。 您可以确定使用的链码版本正确吗?

如果您更新了链码,请检查是否已使用正确的源代码和版本号运行peer chaincode installpeer chaincode upgrade