首页 > 解决方案 > 如何将多个参数传递给 hyperledger-composer 查询?

问题描述

我仍然是区块链开发的初学者,我无法弄清楚我的代码有什么问题。

很简单,我想在这里使用两个参数查询资产。

我什至尝试对这些值进行硬编码,并且它在一个参数查询中运行良好。

使用休息服务器也可以正常工作。

**
* Vote transaction
* @param {org.evotedapp.biznet.GenerateElectionResult} result
* @transaction
*/

async function electionResult(result) {


var factory = getFactory();
var namespace = "org.evotedapp.biznet";    
var currentElection;

const votes = await query("getBallotsByElection", {election: 
"resource:org.evotedapp.biznet.Election#el892076"})  // this query works fine

return getAssetRegistry("org.evotedapp.biznet.Election")
        .then(registry => {
            var electionRegistry = registry.get(result.electionId);
            return electionRegistry
        })
        .then(election => {
            currentElection = election;
            var candidates = election.candidates
            const results = []; 
            for(let i = 0; i<candidates.length;i++) {

                var candidateVotes = query("getBallotsByElectionAndCandidate", {election: `resource:org.evotedapp.biznet.Election#${result.electionId} `,candidate:`${candidates[i]}`})
                var resultCount = factory.newResource(namespace, "ResultCount", result.electionId+i);
                resultCount.candidate = candidates[i];
                if(candidateVotes == []){
                    resultCount.votes = 0
                }else{
                    resultCount.votes = candidateVotes.length;
                }
                results.push(resultCount)

           }

            var voteCountCandidate;
            return getAssetRegistry("org.evotedapp.biznet.VoteCountCandidate")
                .then(registry => {
                    voteCountCandidate = factory.newResource(namespace, "VoteCountCandidate", "vcc_"+result.electionId);
                    voteCountCandidate.election = currentElection
                    voteCountCandidate.results = results;
                    return registry.add(voteCountCandidate)
                })
                .then(() => {
                    var voteCountCandidateEvent = factory.newEvent(namespace, "GenerateElectionResultNotification");
                    voteCountCandidateEvent.voteCountCandidate = voteCountCandidate;

                    emit(voteCountCandidateEvent);
                });
            })

}

这是查询:

query getBallotsByElectionAndCandidate {

    description: "Select all ballots by election"
    statement:
        SELECT org.evotedapp.biznet.Ballot
            WHERE (election == _$election AND votedCandidate == _$candidate)
}

我希望此查询将匹配值返回给候选投票。它使事务失败并给出此错误:

Error: Error trying invoke business network with transaction id 5f256b4ed096962a60e2753f611cad7d051abd4703dde36cb03da08b14036788. 
Error: No valid responses from any peers. Response from attempted peer comms was an error:
Error: transaction returned with failure: 
ValidationException: Instance org.evotedapp.biznet.ResultCount#el9888210 missing required field votes`enter code here`

标签: javascripthyperledger-composer

解决方案


推荐阅读