I am new to MongoDB and working on small tutorial to create associations between tables in MongoDB.
As per the tutorial, we need to create association between three tables. 1. User table 2. BlogPost table 3. Comment table
User may have multiple blogpost and a blogpost may have list of comments and comment is also mapped with user.
User -> BlogPost -> Comment
I have written following test case to create three records and to test the association :
const assert = require('assert');
const User = require('../src/users');
const BlogPost = require('../src/blogPost');
const Comment = require('../src/comment');
describe('Association', () => {
let ascUer, blogPost, comment;
beforeEach( (done) => {
ascUer = new User({name:'associationUser'});
blogPost = new BlogPost({title:'JS is great',content:'Yep, It is !!'});
comment = new Comment({content:'Congratulation for the great poost !!!'});
ascUer.blogPosts.push(blogPost);
blogPost.comments.push(comment);
comment.user = ascUer;
/*
// THIS IS NOT WORKING
Promise.all([ascUer.save(),blogPost.save(),comment.save()])
.then(() => done());
*/
ascUer.save()
.then(() => {
console.log('***********Association user saved ***************');
blogPost.save()
.then(() => {
console.log('***********Blogpost saved ***************');
comment.save()
.then(() => {
console.log('***********Comment saved ***************');
done();
});
});
});
});
it.only('Saves a relation between a user and a blogpost', (done) => {
User.findOne({name:'associationUser'})
.populate('blogPosts')
.then((user) => {
console.log(user);
done();
});
});
});
I am facing weird behavior while running the test case with help of mocha and nodeJs. The test case is getting executed successfully but only "user" table was created while using "Promise.All" feature of ES6. I have commented out "Promise.All" in above code snippet and one by one saving each of three records.
Below image show result of test case execution:
Below image shows snap of RoboMongo tool where only "user" table is present :
Why ES6 "Promise.All" is not working properly in my system ? Any suggestions appreciated. You may also refer my code over GitHub : https://github.com/shahgunjan07/MongoDBTutorial.git for further details.
via Gunjan Shah
No comments:
Post a Comment