Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit ebd9025

Browse files
author
Ace Nassri
committed
Workaround for id field not being populated on books
1 parent 496b536 commit ebd9025

8 files changed

Lines changed: 22 additions & 16 deletions

File tree

complete/app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ app.use(session({ signed: true, secret: config.cookieSecret }));
3535

3636
/* Fetch all books and display them */
3737
app.get('/', function(req, res, next) {
38-
books.getAllBooks(function(err, books) {
38+
books.getAllBooks(function(err, books, key) {
3939
if (err) return next(err);
40-
res.render('index', { books: books, user: req.session.user });
40+
var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
41+
res.render('index', { books: keyBooks, user: req.session.user });
4142
});
4243
});
4344

4445
/* Fetch books created by the currently logged in user and display them */
4546
app.get('/mine', function(req, res, next) {
4647
if (! req.session.user) return res.redirect('/');
47-
books.getUserBooks(req.session.user.id, function(err, books) {
48+
books.getUserBooks(req.session.user.id, function(err, books, key) {
4849
if (err) return next(err);
49-
res.render('index', { books: books, user: req.session.user });
50+
var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
51+
res.render('index', { books: keyBooks, user: req.session.user });
5052
});
5153
});
5254

complete/books.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ module.exports = function(config) {
3131

3232
function getAllBooks(callback) {
3333
var query = datastore.createQuery(['Book']);
34-
datastore.runQuery(query, callback);
34+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
3535
}
3636

3737
function getUserBooks(userId, callback) {
3838
var query = datastore.createQuery(['Book']).filter('userId', '=', userId);
39-
datastore.runQuery(query, callback);
39+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
4040
}
4141

4242
function addBook(title, author, coverImageData, userId, callback) {
4343
var entity = {
4444
key: datastore.key('Book'),
4545
data: {
4646
title: title,
47-
author: author,
47+
author: author
4848
}
4949
};
5050

start/app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ app.use(session({ signed: true, secret: config.cookieSecret }));
3737
app.get('/', function(req, res, next) {
3838
books.getAllBooks(function(err, books) {
3939
if (err) return next(err);
40-
res.render('index', { books: books, user: req.session.user });
40+
var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
41+
res.render('index', { books: keyBooks, user: req.session.user });
4142
});
4243
});
4344

@@ -46,7 +47,8 @@ app.get('/mine', function(req, res, next) {
4647
if (! req.session.user) return res.redirect('/');
4748
books.getUserBooks(req.session.user.id, function(err, books) {
4849
if (err) return next(err);
49-
res.render('index', { books: books, user: req.session.user });
50+
var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
51+
res.render('index', { books: keyBooks, user: req.session.user });
5052
});
5153
});
5254

step-1-retrieve-books/books.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function(config) {
2424

2525
function getAllBooks(callback) {
2626
var query = datastore.createQuery(['Book']);
27-
datastore.runQuery(query, callback);
27+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
2828
}
2929

3030
function getUserBooks(userId, callback) {

step-2-create-and-delete-books/books.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function(config) {
2424

2525
function getAllBooks(callback) {
2626
var query = datastore.createQuery(['Book']);
27-
datastore.runQuery(query, callback);
27+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
2828
}
2929

3030
function getUserBooks(userId, callback) {

step-3-book-cover-images/books.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = function(config) {
3131

3232
function getAllBooks(callback) {
3333
var query = datastore.createQuery(['Book']);
34-
datastore.runQuery(query, callback);
34+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
3535
}
3636

3737
function getUserBooks(userId, callback) {

step-5-user-books/books.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ module.exports = function(config) {
3131

3232
function getAllBooks(callback) {
3333
var query = datastore.createQuery(['Book']);
34-
datastore.runQuery(query, callback);
34+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
3535
}
3636

3737
function getUserBooks(userId, callback) {
3838
var query = datastore.createQuery(['Book']).filter('userId', '=', userId);
39-
datastore.runQuery(query, callback);
39+
datastore.runQuery(query, (err, books) => callback(err, books, datastore.KEY));
4040
}
4141

4242
function addBook(title, author, coverImageData, userId, callback) {

step-6-deploy/app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ app.use(session({ signed: true, secret: config.cookieSecret }));
3737
app.get('/', function(req, res, next) {
3838
books.getAllBooks(function(err, books) {
3939
if (err) return next(err);
40-
res.render('index', { books: books, user: req.session.user });
40+
var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
41+
res.render('index', { books: keyBooks, user: req.session.user });
4142
});
4243
});
4344

@@ -46,7 +47,8 @@ app.get('/mine', function(req, res, next) {
4647
if (! req.session.user) return res.redirect('/');
4748
books.getUserBooks(req.session.user.id, function(err, books) {
4849
if (err) return next(err);
49-
res.render('index', { books: books, user: req.session.user });
50+
var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
51+
res.render('index', { books: keyBooks, user: req.session.user });
5052
});
5153
});
5254

0 commit comments

Comments
 (0)