Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to pass json object from Hyperledger SDK (written in javascript) to Chaincode (written in Go)? My data structure looks like this in the chaincode:

type Field struct {
    Key   string `json:"key"`
    Value string `json:"value"`
}

type File struct {
    Creator string  `json:"creator"`
    Fields  []Field `json:"field"`
}

A file has a creator and an arbitrary number of fields. The idea is to have a general data struct that is flexible enough to save any type of data. For example if I wanted to save a book record:

fields1 := []Field{
        Field{Key: "Book Titel", Value: "Writings 1909-1911"},
        Field{Key: "Publish Date", Value: "13.02.1994"},
        Field{Key: "ISBN", Value: "9780691102504"},
        Field{Key: "Pages", Value: "447"}}
File{Creator: "Einstein", Fields: fields1}

I had success for adding a base set of records to the ledger with the help of Chaincode but I can't invoke the function below from javascript SDK to add new records.

// CreateFile adds a new file to the world state with given details (chaincode)
func (s *SmartContract) CreateFile(ctx contractapi.TransactionContextInterface, fileNumber string, creator string, fields []Field) error {
    file := File{
        Creator: creator,
        Fields:  fields,
    }

    fileAsBytes, _ := json.Marshal(file)

    return ctx.GetStub().PutState(fileNumber, fileAsBytes)
}

Invoke from Javascript SDK

    // connect to network, gateway, etc.
    ..
    const fields = [
    {
      "key": "Event Name",
      "value": "Birthday"
    },
    {
      "key": "Date",
      "value": "05.01.2021"
    }
  ]

await contract.submitTransaction('CreateFile', 'FILE4', 'Me', fields);

If I invoke the function I receive the following error:

error: [Transaction]: Error: No valid responses from any peers. Errors:
    peer=peer0.org1.example.com:7051, status=500, message=Error managing parameter param2. Conversion error. Value [object Object],[object Object] was not passed in expected format []main.Field
    peer=peer0.org2.example.com:9051, status=500, message=Error managing parameter param2. Conversion error. Value [object Object],[object Object] was not passed in expected format []main.Field

I would like to know how can I fix this? thanks in advance

question from:https://stackoverflow.com/questions/65644233/pass-json-object-in-hyperldger-fabric

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
87 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...