Json 使用sails js填充模型的模型
我试图用帆填充模型的模型,不幸的是,它不起作用 我有三种型号Json 使用sails js填充模型的模型,json,node.js,sails.js,Json,Node.js,Sails.js,我试图用帆填充模型的模型,不幸的是,它不起作用 我有三种型号 /** Conversation.js **/ module.exports = { autoCreatedAt: false, autoUpdatedAt: false, tableName:'conversation', attributes: { idConversation:{ columnName:'IDCONVERSATION',
/**
Conversation.js
**/
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
tableName:'conversation',
attributes: {
idConversation:{
columnName:'IDCONVERSATION',
primaryKey:true,
autoIncrement:true,
unique:true,
type:'integer',
index:true
},
dateStartConversation:{
columnName:'DATEDEBUT',
type:'date',
index:true
},
user1:{
columnName:'IDUSER1',
model:'user',
notNull:true
},
user2:{
columnName:'IDUSER2',
model:'user',
notNull:true
},
article:
{
model:'article',
columnName:'IDARTICLE',
notNull:true
}
}
};
/**
Article.js
**/
module.exports = {
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
tableName:'article',
attributes: {
idArticle:{
type:'integer',
unique:true,
columnName:'IDARTICLE',
autoIncrement:true,
primaryKey:true
},
title:{
type:'string',
required:true,
columnName:'TITRE',
index:true,
notNull:true
},
utilisateur:{
model:'utilisateur',
columnName:'IDUTILISATEUR',
required:true,
notNull:true,
dominant:true
},
images:{
collection:'image',
via:'article'
},
conversation:{
collection:'conversation',
via:'article'
}
}
};
/**
Image.js
**/
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
tableName:'image',
attributes: {
idImage:{
columnName:'IDIMAGE',
primaryKey:true,
autoIncrement:true,
unique:true,
type:'integer'
},
pathImage:{
columnName:'PATHIMAGE',
required:true,
type:'string',
notNull:true
},
article:{
model:'article',
columnName:'IDARTICLE',
notNull:true,
dominant:true
}
}
};
正如您在我的模型中看到的,两个用户之间的对话,关于一篇文章,而这些文章CA有一个或多个图像
所以我想得到一个用户的所有对话,我可以用文章填充,但我不能用下面的图片填充文章
Conversation.find().populate('article').populate('user1').populate('user2').where({
or : [
{ user1: iduser },
{ user2: iduser }
]})
.then(function( conversations) {
var i=0;
conversations.forEach(function(element,index){
i++;
console.log("article "+index+" "+JSON.stringify(element.article));
Article.findOne({
idArticle:element.article.idArticle
}).populate('images').then(function(newArticle){
//I try to set article with the newArticle but it don't work
element.article=newArticle;
})
if(i==conversations.length){
res.json({
hasConversation:true,
conversation:conversations
});
}
});
})
因为使用sails无法进行深度填充,所以我尝试使用一个循环来用关联的图像填充每篇文章,并在对话中设置它,但在对话中从未设置过文章
如何修复它?根据判断,如果(I==conversations.length)
在最后,您似乎有一点需要编写异步代码。但是您正在同步forEach
循环中迭代i
,因此您的响应发生在任何数据库查询运行之前。将i++
和if
移动到文章回调的内部。findOne
:
Conversation.find().populate('article').populate('user1').populate('user2').where({
or : [
{ user1: iduser },
{ user2: iduser }
]})
.then(function( conversations) {
var i=0;
conversations.forEach(function(element,index){
console.log("article "+index+" "+JSON.stringify(element.article));
Article.findOne({
idArticle:element.article.idArticle
}).populate('images').then(function(newArticle){
// Associate the article with the conversation,
// calling `toObject` on it first
element.article= newArticle.toObject();
// Done processing this conversation
i++;
// If we're done processing ALL of the conversations, send the response
if(i==conversations.length){
res.json({
hasConversation:true,
conversation:conversations
});
}
})
});
})
在将newArticle
实例分配给对话之前,您还需要调用toObject
,因为它包含images
属性上的getter和setter,在复制时会意外地发生行为
我还建议对其进行重构以使用,这将使其更具可读性。在解决此问题之前(),您可以使用我开发的此函数来解决此问题:您好sgress454,我已经尝试了您的建议,但结果仍然相同,对话中的当前文章也没有使用NewArticle更新,你能用async.each重构代码来更新你的anwser吗?对不起,我忘了在
newArticle
实例上添加toObject
调用。用一些解释更新了代码。正如你所说的那样,每一个都是一个重构。我只是建议它可以使代码更具可读性,但没有必要让它起作用。