Yash Damani
Yash Damani

Reputation: 125

getQueryResult returns '[object Object]' instead of a valid json [hyperledger fabric]

Contract Code:

async function queryCar(ctx, query) {

        let queryResult = await.ctx.stub.getQueryResult(query);
        console.log(queryResult.toString());

        return queryResult.toString();
        }

How I am invoking the function in my API:

 var stringQuery = `{"selector": {"id": "17"}}`
    newQuery = await contract.evaluateTransaction('richQuery',stringQuery);
    console.log(JSON.parse(newQuery));

Isn't the value of stringQuery a valid query to CouchDB?

Maybe I am using the function wrong because the documentation speaks about the function returning a StateQueryIterator. Can anyone help me out on how to use the getQueryResult function in javascript? Thanks!

Upvotes: 0

Views: 2603

Answers (2)

Yash Damani
Yash Damani

Reputation: 125

It's solved now! The issue was with me not knowing how to iterate over the keys it returns properly. Its return type is StateQueryIterator, and I was not using it right. Fixed that, and I got the result.

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

It's because queryResult is an object, and Object.toString() returns [object Object]. Use JSON.stringify to convert the object to a string, then JSON.parse it to get the object back:

return JSON.stringify(queryResult);

And:

console.log(JSON.parse(newQuery));

Upvotes: 2

Related Questions