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
• 26,740 points
1,199 views

1 answer to this question.

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,370 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 8, 2018 in Blockchain by Johnathon
• 9,090 points
802 views
+1 vote
0 answers
+16 votes
4 answers
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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,370 points
633 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,370 points
1,029 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP