[英]Function that queries DB with Knex.js returns undefined
我有一個Node.js服務器,它將用於處理網站。 我正在嘗試制作一個API與數據庫進行交互。 我必須使用Knex.js來管理數據庫。
Knex的初始化以及數據庫及其單個表的創建(目前)都很好。 我在這里試圖做的是一個函數,給定一個id作為參數,它將查找相應的行並返回它。
在這里我創建表:
// create tables in the database
function createDoctorsTable() {
jcdb.schema.hasTable("doctors").then(function (exists) {
if (!exists) {
jcdb.schema.createTable("doctors", function (table) {
table.integer("id");
table.string("name").notNullable();
table.string("surname").notNullable();
table.string("presentation");
table.string("professional_story");
table.string("photo");
table.string("mail");
table.string("phone");
}).then(function () {
return Promise.all(
_.map(doctorsDBfile, function (v) {
return jcdb("doctors").insert(v);
})
);
});
}
else {
return true;
}
});
}
這就是我要嘗試的功能。
function getDoctorById(id) {
var result;
jcdb("doctors").where("id", id)
.then(function(query) {
result = JSON.stringify(query);
});
return result;
}
然后我稱呼我需要的一切:
app.set("port", serverPort);
init_jcdb();
createDoctorsTable();
/* Start the server on port 3000 */
app.listen(serverPort, function() {
console.log(`Your app is ready at port ${serverPort}`);
console.log(getDoctorById(2));
});
調用getDoctorById()返回未定義! 雖然如果我將console.log()放在函數中,它將正確打印該行。 我究竟做錯了什么?
PS:我是js的入門者,就在昨天,我第一次看到它。
function getDoctorById(id) {
var result;
jcdb("doctors").where("id", id)
.then(function(query) {
result = JSON.stringify(query);
});
return result;
}
請嘗試:
function getDoctorById(id) {
return jcdb("doctors").where("id", id)
.then(function(query) {
var result = JSON.stringify(query);
return result;
});
}
承諾讓您更輕松地使用異步代碼。 使用then
,您實際上是在調用一個回調函數,並將鏈中上一個promise的結果作為回調函數的參數。
但是,為此,您必須從promise的回調(您傳遞給then
的函數)內返回一個值,該值將在回調的參數中使用,
並且還返回了promise-returning-functions本身(在本例中為jcdb
)-因為,請記住,只有promise才具有then
。
按照相同的邏輯,您不能像在此處那樣console.log()
函數:
console.log(getDoctorById(2));
因為請記住,該函數返回的是promise
,而不是值。 它間接返回值作為回調函數的參數。 這給了我們:
getDoctorById(2).then(function(result) {
console.log("result:", result);
})
最后一件事,第一個函數createDoctorsTable()
也是異步的。 在某些情況下,它可能在getDoctorById()
之前執行。
因此,首先讓我們從您的createDoctorsTable()
返回一個promise,以便使getDoctorById
嚴格在它之后運行:
function createDoctorsTable() {
return jcdb.schema.hasTable("doctors").then(function (exists) {
.......
(注意:我只添加了return語句,其余的看起來不錯)。
然后更改您的app.listen
,以使最終代碼看起來像這樣(假設init_jcdb()
返回一個promise):
// create tables in the database
function createDoctorsTable() {
return jcdb.schema.hasTable("doctors").then(function (exists) {
if (!exists) {
jcdb.schema.createTable("doctors", function (table) {
table.integer("id");
table.string("name").notNullable();
table.string("surname").notNullable();
table.string("presentation");
table.string("professional_story");
table.string("photo");
table.string("mail");
table.string("phone");
}).then(function () {
return Promise.all(
_.map(doctorsDBfile, function (v) {
return jcdb("doctors").insert(v);
})
);
});
}
else {
return true;
}
});
}
function getDoctorById(id) {
return jcdb("doctors").where("id", id)
.then(function(query) {
var result = JSON.stringify(query);
return result;
});
}
app.set("port", serverPort);
init_jcdb()
.then(function() {
return createDoctorsTable();
})
.then(function() {
getDoctorById(2).then(function(result) {
console.log("result:", result);
})
})
.then(function() {
/* Start the server on port 3000 */
app.listen(serverPort, function() {
console.log(`Your app is ready at port ${serverPort}`);
}
});
});
編寫此代碼有很多變體。
我建議您閱讀有關JavaScript異步模型的更多信息,並使用JavaScript做出承諾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.