An issue has arisen
Uncaught (in promise) TypeError: cb is not a function
The error occurs in db.get()
because the method is being called without a callback parameter (no parameters at all).
In the logIn method, there are two calls to db.get
, first one here
db.get(table.users, (err, info) => { // <-- Pouch db get() function to access data
...
});
and the second call happens here
db.get(); // <-- Here I attempt another get() function call
The second call fails immediately because it seems like
db.get(table.users,(err,info) =>
is defining
db.get
but it's actually an executed call.
Below you can see an example of db.get
with a callback. An async/await example is also included. Refer to 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);
// Retrieve 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 = `👍 Welcome ${userName}!`;
} else {
view.innerText = `👎 Log in failed, try again.`;
}
}
});
}
async function logInAwait(userName, password) {
const view = gel(g_result);
let text = "";
try {
let doc = await db.get("Users");
let info = doc.data.find(e => e.name === userName && e.pass === password);
if (info) {
text = `👍 Welcome ${userName}!`;
} else {
text = `👎 Log in failed, try again.`;
}
} catch (err) {
text = JSON.stringify(err, undefined, 3);
} finally {
view.innerText = text;
}
}
// Sample documents
function getDocsToInstall() {
return [{
_id: "Users",
data: [{
name: "Jerry",
pass: "Garcia"
},
{
name: "Bob",
pass: "Weir"
},
{
name: "Wavy",
pass: "Gravy"
},
]
}];
}
// Initialize database instance
async function initDb() {
db = new PouchDB('test', {
adapter: 'memory'
});
await db.bulkDocs(getDocsToInstall());
};
(async() => {
await initDb();
gel("form").style = "";
})();
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb-7.1.1.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script>
<pre id="form" style="display: none">
<label for="user">User Name</label>
<input id="user" />
<label for="pass">Password</label>
<input id="pass" /> <br/>
<button onclick="logIn(gel('user').value,gel('pass').value)">Log In (callback)</button> <button onclick="logInAwait(gel('user').value,gel('pass').value)">Log In (async)</button>
</pre>
<hr/>
<pre id='result'></pre>