使用 NodeJS 中的承诺将两个对象从两个不同的 SQL 查询传递到 EJS 视图
Passing two objects to EJS view from two different SQL queries using promisses in NodeJS
我正在尝试构建一个包含三个字段的 EJS 表单,我需要同时向其传递两组数据:使用 promises 的用户和书籍。不幸的是,书籍没有通过并保留 'undefined'。我不知道为什么。
表格
- 文本域(与本例无关)
- 带有用户列表的下拉框
- 带有图书列表的下拉框
对于 (2) 和 (3),我查询我的 mysql 数据库以获取数据,以便我可以填写表单下拉框。
/controller.js
const User = require('../models/user.js');
const Book = require('../models/book.js');
exports.getAddNeueAusleihe = (req, res, next) => {
// storing users and books in this object
let view_data_for_my_view = {};
// fetch users for dropdown-box nr.1
User.fetchAll()
.then(([users_rows]) => {
view_data.users = users_rows;
// fetch books for dropdown-box nr. 2
return Book.fetchAll(([books_rows]) => {
view_data.books = books_rows;
});
})
.then(() => {
// send data to view
res.render('neue-ausleihe.ejs', {
users: view_data.users,
books: view_data.books,
pageTitle: 'Neue Ausleihe'
});
});
}
User-fetch 工作正常。但是 Book-fetch 确实 return “undefined”,尽管 books 模型中的 SQL 代码工作正常。它实际上跳入了书籍模型,但没有获取视图的值。这是我的 SQL-模型代码。
/models/user.js
const db = require('../util/database.js');
module.exports = class User {
constructor(id, name) {
this.id = id;
this.name = name;
}
static fetchAll() {
return db.execute('SELECT * from dbuser WHERE UserFlag = "active"');
};
}
/models/books.js
const db = require('../util/database.js');
module.exports = class Book {
constructor(id, name) {
this.id = id;
this.name = name;
}
static fetchAll() {
return db.execute('SELECT * from books WHERE status = "free"');
}
}
假设 db.execute
returns 一个解析为数组的承诺,第一个条目是实际结果,您的代码应该看起来更像这样:
exports.getAddNeueAusleihe = (req, res, next) => {
// storing users and books in this object
const view_data = {};
// fetch users for dropdown-box nr.1
User.fetchAll()
.then(([users_rows]) => {
view_data.users = users_rows;
// fetch books for dropdown-box nr. 2
return Book.fetchAll();
})
.then([book_rows] => {
view_data.books = books_rows;
})
.then(() => {
// send data to view
res.render('neue-ausleihe.ejs', {
users: view_data.users,
books: view_data.books,
pageTitle: 'Neue Ausleihe'
});
});
}
奖励版本:
exports.getAddNeueAusleihe = async(req, res, next) =>
// send data to view
res.render('neue-ausleihe.ejs', {
pageTitle: 'Neue Ausleihe',
users: (await User.fetchAll())[0]
books: (await Book.fetchAll())[0]
});
}
即使是写这篇文章的人,它仍然让我震惊 async/await-based 代码比 promise 链更具可读性。
我正在尝试构建一个包含三个字段的 EJS 表单,我需要同时向其传递两组数据:使用 promises 的用户和书籍。不幸的是,书籍没有通过并保留 'undefined'。我不知道为什么。
表格
- 文本域(与本例无关)
- 带有用户列表的下拉框
- 带有图书列表的下拉框
对于 (2) 和 (3),我查询我的 mysql 数据库以获取数据,以便我可以填写表单下拉框。
/controller.js
const User = require('../models/user.js');
const Book = require('../models/book.js');
exports.getAddNeueAusleihe = (req, res, next) => {
// storing users and books in this object
let view_data_for_my_view = {};
// fetch users for dropdown-box nr.1
User.fetchAll()
.then(([users_rows]) => {
view_data.users = users_rows;
// fetch books for dropdown-box nr. 2
return Book.fetchAll(([books_rows]) => {
view_data.books = books_rows;
});
})
.then(() => {
// send data to view
res.render('neue-ausleihe.ejs', {
users: view_data.users,
books: view_data.books,
pageTitle: 'Neue Ausleihe'
});
});
}
User-fetch 工作正常。但是 Book-fetch 确实 return “undefined”,尽管 books 模型中的 SQL 代码工作正常。它实际上跳入了书籍模型,但没有获取视图的值。这是我的 SQL-模型代码。
/models/user.js
const db = require('../util/database.js');
module.exports = class User {
constructor(id, name) {
this.id = id;
this.name = name;
}
static fetchAll() {
return db.execute('SELECT * from dbuser WHERE UserFlag = "active"');
};
}
/models/books.js
const db = require('../util/database.js');
module.exports = class Book {
constructor(id, name) {
this.id = id;
this.name = name;
}
static fetchAll() {
return db.execute('SELECT * from books WHERE status = "free"');
}
}
假设 db.execute
returns 一个解析为数组的承诺,第一个条目是实际结果,您的代码应该看起来更像这样:
exports.getAddNeueAusleihe = (req, res, next) => {
// storing users and books in this object
const view_data = {};
// fetch users for dropdown-box nr.1
User.fetchAll()
.then(([users_rows]) => {
view_data.users = users_rows;
// fetch books for dropdown-box nr. 2
return Book.fetchAll();
})
.then([book_rows] => {
view_data.books = books_rows;
})
.then(() => {
// send data to view
res.render('neue-ausleihe.ejs', {
users: view_data.users,
books: view_data.books,
pageTitle: 'Neue Ausleihe'
});
});
}
奖励版本:
exports.getAddNeueAusleihe = async(req, res, next) =>
// send data to view
res.render('neue-ausleihe.ejs', {
pageTitle: 'Neue Ausleihe',
users: (await User.fetchAll())[0]
books: (await Book.fetchAll())[0]
});
}
即使是写这篇文章的人,它仍然让我震惊 async/await-based 代码比 promise 链更具可读性。