Update one item with localforage
Asked Answered
L

2

7

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?

Leptosome answered 24/9, 2015 at 21:32 Comment(0)
L
4

We find solution :). Thanks Buzinas for inspiration

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

    localforage.setItem('123456', item);

  });
Leptosome answered 29/9, 2015 at 8:48 Comment(0)
O
9

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 );
});
Ovovitellin answered 24/9, 2015 at 21:34 Comment(5)
Thanks Buzinas, unfortunately i've no field update or console log.Leptosome
I didn't understand, @Romain. Didn't my solution work for you?Ovovitellin
It does not work ;( i haven't any error in console, no update on offre_cli itemLeptosome
This won't work because localforage.getItem isn't synchronous.Anglaangle
@Anglaangle You're right, my fault. Fixed the answer, thanks :)Ovovitellin
L
4

We find solution :). Thanks Buzinas for inspiration

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

    localforage.setItem('123456', item);

  });
Leptosome answered 29/9, 2015 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.