返回一个对象给出未定义,使用 db.all() sqlite3 javascript
returning a object gives undefined, using db.all() sqlite3 javascript
我不明白为什么它的控制台记录一个未定义的值而不是对象
function getusername( username ){
let currentUsernameObj;
sqlDB.all(sql, [username], (err, rows) => {
if (err) {
throw err;
}
if (!(rows.length === 0)&& username === rows[0].username) {
console.log(username +" sqn " +rows[0].username );
console.log(rows[0]);
currentUsernameObj=rows[0];
} else {
console.log("user not found")
}
console.log(typeof(currentUsernameObj))
return currentUsernameObj;
})};
var x=getusername("user2@s.com");
console.log(x+" yyyyyy");
这是我的输出
我期待显示一个对象,但不知道该怎么做
您的回调(见下文)将在函数返回后执行,因为您没有将 currentUsernameObj
初始化为任何东西 undefined
。
// this callback is called after SQL returns the data
// as DB access is slow this will only be executed after your function has already returned
(err, rows) => {
if (err) {
throw err;
}
if (!(rows.length === 0)&& username === rows[0].username) {
console.log(username +" sqn " +rows[0].username );
console.log(rows[0]);
currentUsernameObj=rows[0];
} else {
console.log("user not found")
}
console.log(typeof(currentUsernameObj))
return currentUsernameObj;
}
您将不得不等待这些结果。不幸的是,SQLite 没有基于 API 的 Promise 来简化这一切,因此您必须自己创建 Promise。
async function getusername(username) {
// wrap the function in a Promise
// resolve the promise on success
// reject the promise on error
return new Promise((resolve, reject) => {
sqlDB.all(sql, [username], (err, rows) => {
if (err) {
// promise needs to be rejected as an error occurred
reject(err)
}
if (!(rows.length === 0) && username === rows[0].username) {
console.log(username + " sqn " + rows[0].username);
console.log(rows[0]);
currentUsernameObj = rows[0];
} else {
console.log("user not found");
}
console.log(typeof currentUsernameObj);
resolve(currentUsernameObj);
});
});
}
// in order to be able to call an async method with await the call has to be in an async method itself
(async () => {
// we can now use await to wait for the result
var x = await getusername("user2@s.com");
console.log(x + " yyyyyy");
})();
有关 async/ await
以及如何正确处理错误的更多信息,请参阅 JavaScript docs, this tutorial 或详细介绍的无数 YouTube 视频之一。只需搜索 async/ await
或 Promises
.
我不明白为什么它的控制台记录一个未定义的值而不是对象
function getusername( username ){
let currentUsernameObj;
sqlDB.all(sql, [username], (err, rows) => {
if (err) {
throw err;
}
if (!(rows.length === 0)&& username === rows[0].username) {
console.log(username +" sqn " +rows[0].username );
console.log(rows[0]);
currentUsernameObj=rows[0];
} else {
console.log("user not found")
}
console.log(typeof(currentUsernameObj))
return currentUsernameObj;
})};
var x=getusername("user2@s.com");
console.log(x+" yyyyyy");
这是我的输出
我期待显示一个对象,但不知道该怎么做
您的回调(见下文)将在函数返回后执行,因为您没有将 currentUsernameObj
初始化为任何东西 undefined
。
// this callback is called after SQL returns the data
// as DB access is slow this will only be executed after your function has already returned
(err, rows) => {
if (err) {
throw err;
}
if (!(rows.length === 0)&& username === rows[0].username) {
console.log(username +" sqn " +rows[0].username );
console.log(rows[0]);
currentUsernameObj=rows[0];
} else {
console.log("user not found")
}
console.log(typeof(currentUsernameObj))
return currentUsernameObj;
}
您将不得不等待这些结果。不幸的是,SQLite 没有基于 API 的 Promise 来简化这一切,因此您必须自己创建 Promise。
async function getusername(username) {
// wrap the function in a Promise
// resolve the promise on success
// reject the promise on error
return new Promise((resolve, reject) => {
sqlDB.all(sql, [username], (err, rows) => {
if (err) {
// promise needs to be rejected as an error occurred
reject(err)
}
if (!(rows.length === 0) && username === rows[0].username) {
console.log(username + " sqn " + rows[0].username);
console.log(rows[0]);
currentUsernameObj = rows[0];
} else {
console.log("user not found");
}
console.log(typeof currentUsernameObj);
resolve(currentUsernameObj);
});
});
}
// in order to be able to call an async method with await the call has to be in an async method itself
(async () => {
// we can now use await to wait for the result
var x = await getusername("user2@s.com");
console.log(x + " yyyyyy");
})();
有关 async/ await
以及如何正确处理错误的更多信息,请参阅 JavaScript docs, this tutorial 或详细介绍的无数 YouTube 视频之一。只需搜索 async/ await
或 Promises
.