I am using promise-mysql in my application where I need to query the information of a book, and then retrieve some books with the same author as its.
Codes:
return Promise.all([
pool.query(sql, id),
]).then(data=> {
var book = data[0][0];
if (book.author) {
var authorSql = "select * from authors where name = ? limit 10";
return pool.query(authorSql, book.author);
} else {
return Promise.resolve(book);
}
}).then(object=> {
if (object.id) {
// this is a book instance, return it back
return object
} else {
// this should an array of books
var booksWithSameAuthor = object;
// but how to get the origin book here, something like:
var book = xxxx
book.recommends_by_author = booksWithSameAuthor
return book
}
});
As shown, in the second then
function, the object
maybe a book
object or an array like queried data rows, in the later situation, I need the book
object too, but I have no idea how to pass it down.
Any ideas?