Cannot read property 'post' of undefined

TypeError: Cannot read property ‘post’ of undefined
at EmployeeProvider.webpackJsonp.100.EmployeeProvider.create (http://localhost:8100/build/main.js:32:25)
at EmployeePage.webpackJsonp.201.EmployeePage.addOrUpdate (http://localhost:8100/build/main.js:211:35)
at Object.eval [as handleEvent] (ng:///AppModule/EmployeePage.ngfactory.js:129:27)
at handleEvent (http://localhost:8100/build/vendor.js:13963:155)
at callWithDebugContext (http://localhost:8100/build/vendor.js:15472:42)
at Object.debugHandleEvent [as handleEvent] (http://localhost:8100/build/vendor.js:15059:12)
at dispatchEvent (http://localhost:8100/build/vendor.js:10378:25)
at http://localhost:8100/build/vendor.js:11003:38
at HTMLButtonElement. (http://localhost:8100/build/vendor.js:39492:53)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660)

====

import { Injectable } from ‘@angular/core’;
import { Http } from ‘@angular/http’;
import ‘rxjs/add/operator/map’;
import * as PouchDB from ‘pouchdb’;
import cordovaSqlitePlugin from ‘pouchdb-adapter-cordova-sqlite’;

@Injectable()
export class EmployeeProvider {
public pdb;
public employees;

createPouchDB() {
PouchDB.plugin(cordovaSqlitePlugin);
this.pdb = new PouchDB(‘employees.db’,
{ adapter: ‘cordova-sqlite’ });
}

create(employee) {
return this.pdb.post(employee);
}

update(employee) {  
	return this.pdb.put(employee);
} 

delete(employee) {  
	return this.pdb.delete(employee);
} 

read() {
	function allDocs(){
			   this.pdb.allDocs({ include_docs: true})
		.then(docs => {
			this.employees = docs.rows.map(row => {
				row.doc.Date = new Date(row.doc.Date);
				return row.doc;
		 });


			return this.employees;
		});
	  }

/*
this.pdb.changes({ live: true, since: ‘now’, include_docs: true})
.on(‘change’, ()=>{
allDocs().then((emps)=>{

					this.employees = emps;
					});
				});
				*/
	return allDocs();

}

}

Hello,

you have anywhere whatever.post, but whatever does not exist or does not exist at the time you call it. A good candidate is maybe your create routine. Check when you call whatever.post in your lifecycle, maybe your Sqliteplugin is not ready at that time.

Best regards, anna-liebt

Hi,

I am having the same issue … seems using the same tutorial (https://www.techiediaries.com/ionic-sqlite-pouchdb/)

TypeError: Cannot read property 'pdb' of undefined
    at allDocs (http://localhost:8100/build/main.js:444:18)

Since the error is on this, I have used the advice from here
But still having error on other elements (allDocs undefined, etc)

Any help please?

I have check the lifecycle and wondering how to check that SQLiteplugin plugin is ready.

The simplified lifecycle is:

  1. this.employeeProvider.createPouchDB(); succeed … including PouchDB.plugin(cordovaSqlitePlugin);
ionViewDidEnter() {

            this.employeeProvider.createPouchDB();

            this.employeeProvider.read()
                .then(employees => {
                    this.employees = employees;
                })
                .catch((err)=>{});

    }
createPouchDB() {
        PouchDB.plugin(cordovaSqlitePlugin);
        this.pdb = new PouchDB('employees.db', 
        { adapter: 'cordova-sqlite' });
  }
  1. This fails on this.pdb.allDocs({ include_docs: true}) ...
read() {  
        function allDocs(){
                   this.pdb.allDocs({ include_docs: true})
            .then(docs => {
                this.employees = docs.rows.map(row => {
                    row.doc.Date = new Date(row.doc.Date);
                    return row.doc;
             });


                return this.employees;
            });
          }

          this.pdb.changes({ live: true, since: 'now', include_docs: true})
                    .on('change', ()=>{
                        allDocs().then((emps)=>{

                        this.employees = emps;
                        });
                    });
        return allDocs();

}

Thanks for help

1 Like

Never type the word “function” inside the body of one.