0

I'd like to return values with the find module with PouchDb

I can't return values with the find function and PouchDb

If i call the function 'loadLists' i have this error : TypeError: Cannot set property 'lists' of undefined

public lists;

loadLists(culture, code_agresseur) {

    PouchDB.plugin(require('pouchdb-adapter-cordova-sqlite'));
    this.epiphyt= new PouchDB('epiphyt', {adapter: 'cordova-sqlite'});     

                    // Retrouver les protocoles nationaux

                    return this.epiphyt.find({
                    selector: {
                      ProtocoleCultures: {
                        //"ZAK+"
                        $regex:  culture+'+'

                        },
                    LigneOrganismevivantcode: code_agresseur


                    },
        fields: ['ClasseValeurqualitativecode','ClasseValeurqualitative']

                    }).then(function (res) {console.log(res.docs);

                         this.lists= res.docs;

                        }).catch(function (err) {
                    console.log(err);
                    });
                   } 

         // Calling the function 

            this.loadLists('ZAK',8888');
luden
  • 75
  • 1
  • 4
  • Use arrow functions for callback `function (res) {console.log(res.docs); this.lists= res.docs; }` use `(res)=>{}` – Suraj Rao Mar 28 '19 at 14:00

1 Answers1

0

Looking at the code you've supplied, there's no surrounding class. Assuming there is, declare the function as an arrow function instead so that 'this' refers to that class' instance.

loadLists = (culture, code_agresseur) => {
 // code //
};

Also use arrow functions within that function.

Otherwise, if it's not within a class, just prepend the code above with the 'const' and that should probably also work.

Fredrik_Macrobond
  • 1,716
  • 18
  • 27