PouchDB - Call get() in function ( TypeError: cb is not a function )
PouchDB - Call get() in function ( TypeError: cb is not a function )
我正在尝试使用 PouchDB 制作一个简单的登录系统,但是当我想在我的函数 logIn()
中调用 db.get()
时遇到问题
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
});
在控制台中我得到
Uncaught (in promise) TypeError: cb is not a function
有更好的选择吗?
错误
Uncaught (in promise) TypeError: cb is not a function
在db.get()
中抛出,因为代码在调用get方法时没有回调参数(实际上没有参数)。
logIn 方法正在调用 db.get
两次,第一次在这里
db.get(table.users, (err, info) => { // <-- Pouch db get() function to get data
...
});
然后在这里
db.get(); // <-- Here I call get() function
第二次调用立即失败。看起来想法是 db.get(table.users,(err,info) =>
正在定义 db.get
但它不是,这是一个实际的调用。
下面的代码片段演示了 db.get
回调。我在那里留下了一个 async/await 例子。请参阅 get
的 pouchDB 文档
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 = ` 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;
}
}
// canned documents
function getDocsToInstall() {
return [{
_id: "Users",
data: [{
name: "Jerry",
pass: "Garcia"
},
{
name: "Bob",
pass: "Weir"
},
{
name: "Wavy",
pass: "Gravy"
},
]
}];
}
// init example db 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>
我正在尝试使用 PouchDB 制作一个简单的登录系统,但是当我想在我的函数 logIn()
db.get()
时遇到问题
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
});
在控制台中我得到
Uncaught (in promise) TypeError: cb is not a function
有更好的选择吗?
错误
Uncaught (in promise) TypeError: cb is not a function
在db.get()
中抛出,因为代码在调用get方法时没有回调参数(实际上没有参数)。
logIn 方法正在调用 db.get
两次,第一次在这里
db.get(table.users, (err, info) => { // <-- Pouch db get() function to get data
...
});
然后在这里
db.get(); // <-- Here I call get() function
第二次调用立即失败。看起来想法是 db.get(table.users,(err,info) =>
正在定义 db.get
但它不是,这是一个实际的调用。
下面的代码片段演示了 db.get
回调。我在那里留下了一个 async/await 例子。请参阅 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 = ` 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;
}
}
// canned documents
function getDocsToInstall() {
return [{
_id: "Users",
data: [{
name: "Jerry",
pass: "Garcia"
},
{
name: "Bob",
pass: "Weir"
},
{
name: "Wavy",
pass: "Gravy"
},
]
}];
}
// init example db 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>