async changeCarOwner(ctx, carNumber, newOwner) {
const carAsBytes = await ctx.stub.getState(carNumber);
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
const car = JSON.parse(carAsBytes.toString());
car.owner = newOwner
await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
}
}
I keep getting an error: Unexpected end of JSON input. Why? I am trying to update an existing key-value pair in couchDb using the above code.
This error happens at this line:
const car = JSON.parse(carAsBytes.toString());
It is due to the fact that carAsBytes.toString()
does not evaluates to a properly formatted JSON string. The code you show seems fine, but the error is coming from elsewhere in your code.
Debugging tip: use the debugger statement to examine variables before the faulty line, simply add a console.log(carAsBytes.toString())
before it.