Home > other >  PouchDB - Call get() in function ( TypeError: cb is not a function )
PouchDB - Call get() in function ( TypeError: cb is not a function )

Time:09-27

I am trying to make a simple login system using PouchDB, but I have a problem when I want to call db.get() in my function logIn()

  var submit = $("input[name='submit']");
  function logIn() {
    var username = $("input[name='username']").value;
    var password = $("input[name='password']").value;

    db.get(table.users, (err, info) => { // <-- Pouch db get() function to get data
      if (!err) {
        var data = ("db value", info);
        for (i = 0; i < 2; i  ) {
          if (username == data[i].name && password == data[i].pass) {
            console.log(username   " is logged in!");
            return;
          }
        }
        console.log("Incorrect data!");
      } else {
        console.log("err field", err);
      }
    });
    db.get(); // <-- Here I call get() function
  }
  
  submit.click(() => {
    logIn(); // <-- On click call login() function
  });

In the console I get

Uncaught (in promise) TypeError: cb is not a function

is there a better option for this?

CodePudding user response:

The error

Uncaught (in promise) TypeError: cb is not a function

Is thrown in db.get() because the code is calling the get method without a callback parameter (no parameters in fact).

The logIn method is calling db.get twice, first here

db.get(table.users, (err, info) => { // <-- Pouch db get() function to get data
 ...
});

and then here

db.get(); // <-- Here I call get() function

The second call fails immediately. It appears the thinking is db.get(table.users,(err,info) => is defining db.get but it's not, it's an actual call.

The snippet below demonstrates db.get with callback. I left an async/await example in there. See the pouchDB documentation for get

const g_result = 'result';
const gel = id => document.getElementById(id);
let db;

function logIn(userName, password) {
  const view = gel(g_result);
  // get the Users doc using get
  db.get("Users", (err, doc) => {
    if (err) {
      view.innerText = JSON.stringify(err, undefined, 3);
    } else {
      let info = doc.data.find(e => e.name === userName && e.pass === password);
      if (info) {
        view.innerText = `           
  • Related