Question: Sequelize Pulling data from multiple tables and passing as objects to render in EJS

Question

Sequelize Pulling data from multiple tables and passing as objects to render in EJS

Answers 1
Added at 2016-12-25 22:12
Tags
Question

am new to Node Js and associated programmes. Currently trying to create an app which uploads images with captions etc and for users to like and comment (more like instagram/facebook). So far my app works but now stuck at a point when am trying to pull multiple values from multiple tables, passing the values as objects in an array and render in a new EJS file. Below is the code

 app.get('/pages/photos/:id', function (req, res){
//show photos with comments
//1. photo with id :id
// 2. all comments associated with id :id
//likes associated with photo
var photoId=req.params.id;
var photoData = []
Photos.findOne({
    where:{id: photoId}, 
    attributes: ['userId','filename', 'caption', 'createdAt']
}).then(function (rowPhoto){
photoData.photoValues = []
var vals = {
    photoId:photoId,
    userId:rowPhoto.userId,
    photoName: rowPhoto.filename,
    caption:rowPhoto.caption,
    createdAt:rowPhoto.createdAt
}   
photoData.photoValues.push(vals);
console.log("#1 "+ rowPhoto.caption);

}).then(function(){
Comments.findAll(
    {where:
        {photoId:photoId}
    }).then(function(rowComments){
photoData.photoComments= []
for (i=0; i < rowComments.length ; i++){
    vals = {
    id: rowComments[i].id,
    userId: rowComments[i].userId,
    text:rowComments[i].comment,
    createdAt:rowComments[i].createdAt
    }
photoData.photoComments.push(vals);
}
console.log("#2 "+ rowComments.length);
    });
    }).then(function(){
       Likes.findAll({
          where:{photoId:photoId}
}).then(function(rowLikes){
    photoData.photoLikes= []
    for (i=0; i < rowLikes.length ; i++){
    vals = {
    id: rowLikes[i].id,
    liked: rowLikes[i].liked,
    userId:rowLikes[i].userId,
    photoId:rowLikes[i].photoId
    }
   photoData.photoLikes.push(vals);

        }   
    console.log("#3 " + rowLikes[i]);

           });
    });
console.log("#4 " + photoData);
//res.render('views', {views:photoData});
});

is there an easier way to work around this? I haven't touched Migrations yet though.

Answers
nr: #1 dodano: 2016-12-31 19:12
    Photos.findOne({
    where:{id: photoId}, 
    attributes: ['id','userId','filename', 'caption', 'createdAt']
          }).then(function (rowPhotos){
             Comments.findAll({
                    where: {photoId:photoId}
             }).then(function (rowComments){
                Likes.findAll({
                    where:  {photoId: photoId}
                }).then(function (rowLikes){
                    Users.findAll().then(function (commentUser){
            var data= {
            photoData: rowPhotos,
            commentData: rowComments,
            likesData: rowLikes,
            userData:commentUser
            }
           console.log(data);
           res.render('views', {data:data})

                    })

                })
        })  
})

});

This solution by kind courtesy the link below: Querying multiple models with Sequelize.js ORM

Source Show
◀ Wstecz