javascript-从多个表中提取数据并作为对象传递以在EJS中呈现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-从多个表中提取数据并作为对象传递以在EJS中呈现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2217字,纯文字阅读大概需要4分钟。
内容图文
![javascript-从多个表中提取数据并作为对象传递以在EJS中呈现](/upload/InfoBanner/zyjiaocheng/667/67dcf2cdaa1b44a79e2d8a699853ddf3.jpg)
是Node J和相关程序的新手.当前正在尝试创建一个应用,该应用上传带有字幕等的图像,并让用户喜欢和评论(更像instagram / facebook).到目前为止,我的应用程序可以运行,但是现在正试图从多个表中提取多个值,并将这些值作为对象传递到数组中并呈现在新的EJS文件中时停滞了.下面是代码
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});
});
有没有更简单的方法来解决此问题?我还没有接触过迁移.
解决方法:
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})
})
})
})
})
});
此解决方案由以下链接提供:
Querying multiple models with Sequelize.js ORM
内容总结
以上是互联网集市为您收集整理的javascript-从多个表中提取数据并作为对象传递以在EJS中呈现全部内容,希望文章能够帮你解决javascript-从多个表中提取数据并作为对象传递以在EJS中呈现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。