Updating data of struct to save in chaincode.

0 votes

I have created a struct to store some data and i want to implement an update mechanism to it where the data in
the struct is update. I have tried the following code but its not working. How can i do it?


type Customer struct {
  UID     string
  Name    string
  Address struct {
    StreetNo string
    Country  string
  }
}
func (t *SimpleChaincode) update(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {


if len(args) != 2 {
    return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
}
  //args[0] to get the UID of customer whose details to be updated
  //args[1] data that needs to be updated 

data,err:= stub.GetState(ars[0])
if err != nil {
     return nil, err
}
json.Unmarshal(data, &uid)
//append
data.Country= append(data.Country,args[1])
fmt.Printf("Risk Fag set for UID %s",data)

raw, err := json.Marshal(data)
  if err != nil {
     return nil, err
  }

  stub.PutState(data.UID, raw)

return nil,nil

}

Jul 5, 2018 in Blockchain by digger
• 27,620 points
88 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use a code similar to this:
map[string]interface{}

byt := []byte(`{"num":6.13,"strs":["a","b"]}`)
if err := json.Unmarshal(byt, &dat); err != nil {
    panic(err)
}
fmt.Println(dat)

Go through the example of marshalling and unmarshalling structs from/to Json given in the following link:
https://github.com/hyperledger/fabric/blob/release/examples/chaincode/go/marbles02/marbles_chaincode.go

answered Jul 5, 2018 by slayer
• 29,040 points

Related Questions In Blockchain

0 votes
1 answer

How to check the data integrity logic in proof of work mining?

The proof of work is actually works ...READ MORE

answered May 7, 2018 in Blockchain by Johnathon
• 9,050 points
20 views
+16 votes
4 answers
0 votes
1 answer

Where to store the data of decentralized applications??

Every blockchain is developed in a differnet ...READ MORE

answered Apr 20, 2018 in Blockchain by Perry
• 16,450 points
30 views
0 votes
1 answer
0 votes
1 answer

How to track changes of an object in exonum?

You have to describe each component in ...READ MORE

answered Jul 23, 2018 in Blockchain by slayer
• 29,040 points
29 views
0 votes
1 answer

How to get amount of bitcoins in php?

I’ve tried this code and it works: <?php function ...READ MORE

answered Aug 24, 2018 in Blockchain by slayer
• 29,040 points
31 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.