Skip to content

Instantly share code, notes, and snippets.

@austingray
Created August 3, 2015 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austingray/707b7dd4798ed958a638 to your computer and use it in GitHub Desktop.
Save austingray/707b7dd4798ed958a638 to your computer and use it in GitHub Desktop.
comment.js is my model. A comment has a parent_id column that stores the id of the parent comment. In post.js, i build my nested array by calling fetchAll withRelated children.children.children.children. This gives me a nested object with all comments. I'm trying to add an account object to every single comment object, but it is only attaching t…
var bookshelf = require('./bookshelf.js');
var Account = require('./account.js');
var Comment = bookshelf.Model.extend({
tableName: 'comments',
children: function() {
return this.hasMany(Comment, 'parent_id')
},
account: function() {
return this.belongsTo(Account, 'account_id');
}
});
module.exports = Comment;
router.get('/:id', function(req, res, next) {
var post_id = req.params.id
new Post({'uuid': post_id})
.fetch()
.then(function(model){
new Comment()
.query({where: {post_id: post_id}})
.query({where: {parent_id: null}})
.fetchAll({
withRelated: ['children.children.children.children', 'account']
})
.then(function(model){
if (model === null) {
res.render('post', { user: req.user, comments: [] });
} else {
var comments = model.toJSON();
console.log(util.inspect(comments, {showHidden: false, depth: null}));
res.render('post', { user: req.user, comments: comments });
}
});
});
});
//here is the console.log from line 17 of post.js
[ { id: 77,
post_id: '27b197b1-50c8-41f1-8312-95d243b42cc8',
account_id: 0,
comment_body: 'comment',
posted_date: Mon Aug 03 2015 06:38:35 GMT-0400 (EDT),
parent_id: null,
author_alias: null,
children:
[ { id: 79,
post_id: '27b197b1-50c8-41f1-8312-95d243b42cc8',
account_id: 33,
comment_body: 'NULL!?',
posted_date: Mon Aug 03 2015 06:46:36 GMT-0400 (EDT),
parent_id: 77,
author_alias: 'admin',
children:
[ { id: 82,
post_id: '27b197b1-50c8-41f1-8312-95d243b42cc8',
account_id: 35,
comment_body: 'Sup admin?',
posted_date: Mon Aug 03 2015 06:56:37 GMT-0400 (EDT),
parent_id: 79,
author_alias: 'YahoO',
children: [] } ] } ],
account: {} },
{ id: 80,
post_id: '27b197b1-50c8-41f1-8312-95d243b42cc8',
account_id: 34,
comment_body: 'This is posted by the user YAHOO!!!! SUP BRA?',
posted_date: Mon Aug 03 2015 06:55:17 GMT-0400 (EDT),
parent_id: null,
author_alias: 'yahoo',
children:
[ { id: 81,
post_id: '27b197b1-50c8-41f1-8312-95d243b42cc8',
account_id: 35,
comment_body: 'Hrmm, I better make these accounts case insensitive.',
posted_date: Mon Aug 03 2015 06:56:29 GMT-0400 (EDT),
parent_id: 80,
author_alias: 'YahoO',
children: [] } ],
account:
{ id: 34,
alias: 'yahoo',
hash: '$2a$10$lOq8hEuhSZlyFSN/.AFHreZJkdRlLgMIAObupBkjbxUPDgkHCPmMy',
created_on: Mon Aug 03 2015 06:54:38 GMT-0400 (EDT),
email: 'null@null.com',
privacy: 0,
profile_photo: null } } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment