【问题标题】:Cannot set property of undefined with PouchDb when calling a function [duplicate]调用函数时无法使用 PouchDb 设置未定义的属性 [重复]
【发布时间】:2019-03-28 22:02:25
【问题描述】:

我想用 PouchDb 的 find 模块返回值

我无法使用 find 函数和 PouchDb 返回值

如果我调用函数 'loadLists' 我有这个错误: TypeError:无法设置未定义的属性“列表”

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');

【问题讨论】:

  • 使用箭头函数回调function (res) {console.log(res.docs); this.lists= res.docs; }使用(res)=>{}

标签: typescript ionic-framework pouchdb


【解决方案1】:

查看您提供的代码,没有周围的类。 假设存在,将函数声明为箭头函数,以便“this”引用该类的实例。

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

也在该函数中使用箭头函数。

否则,如果它不在一个类中,只需在上面的代码前面加上“const”,这应该也可以工作。

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多