Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update one item with localforage

I've a local indexeddb like this:

// Unlike localStorage, you can store non-strings.
localforage.setItem('123456', {
  //0 - Ref cli
  ref_cli: '4720271',
  //1 - Offre client
  offre_cli: 'Offre 3',
  //2 - Date RDV
  date_rdv: '22/09/2015',
  //3 - Heure rdv
  heure_rdv: '10H00',
  //4 - Nom client
  nom_cli: 'DURAND',
  //5 - Prénom client
  pren_cli: 'Pierre',
  //6 -Adresse client
  adr_cli: '3 rue de l\'église',
  //7 - CP client
  cp_cli: '75000',
  //8 - Ville client
  ville_cli: 'PARIS',
  //9 - Batiment
  bat_cli: 'A',
  //10 - Escalier
  esc_cli: '2',
  //11 - Etage
  etg_cli: '3'
}, function(err, value) { });

I want to update one item of this array. Eg:

offre_cli: 'offre 3' -> offre_cli: 'offre 4'

I've tried the code below, but it erases the previously array.:

localforage.setItem('123456', {offre_cli: 'offre 4'};

It's possible to update a single item, without update all array?

like image 468
Romain Avatar asked Sep 24 '15 21:09

Romain


2 Answers

You can get the current object saved into localforage, update it, and save it back. E.g:

localforage.getItem('123456').then(function (item) {
  item.offre_cli = 'offre 4';
  localforage.setItem('123456', item );
});
like image 68
Buzinas Avatar answered Oct 10 '22 18:10

Buzinas


We find solution :). Thanks Buzinas for inspiration

  localforage.getItem('123456').then(function ( item ){
    item.offre_cli = 'offre 12';

    localforage.setItem('123456', item);

  });
like image 23
Romain Avatar answered Oct 10 '22 17:10

Romain