Updating an entry in PersistentVector not working NEAR Protocol

Viewed 56

I'm trying to update the status of a job object. I get the "success" message return but the value is not updating. Do I miss something?

@nearBindgen
export class Contract {
  private jobs: PersistentVector<Job> = new PersistentVector<Job>('jobs');

......

@mutateState()
  cancelJob(jobTitle: string): string {
    for (let i = 0; i < this.jobs.length; i++) {
      if (this.jobs[i].title == jobTitle) {
          this.jobs[i].status = "Cancelled";
        return "success"
      }
    }
    return "not found";
  }

And I'm calling it like that:

near call apptwo.msaudi.testnet cancelJob '{\"jobTitle\":\"title2\"}' --account-id=msaudi.testnet
1 Answers

It’s not enough to update entry when you fetch it. You need to update the storage on the contract as well. Write it back in so to speak.

This isn’t enough

this.jobs[i].status = "Cancelled";

You need to add it back in:

if (this.jobs[i].title == jobTitle) {
  const job: Job = this.jobs[i]; // Need an intermediate object in memory
  job.status = "Cancelled";
  this.jobs.replace(i, job); // Update storage with the new job.
  return "success"
}
Related