【问题标题】:Populate model of the model with sails js用sails js填充模型的模型
【发布时间】:2015-11-05 11:38:00
【问题描述】:

我正在尝试用帆填充模型的模型,不幸的是它不起作用。

我有 3 个模型

/**
  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

        }
    }
};

正如您在我的模型中看到的那样,两个用户之间的对话,关于一篇文章,而这些文章 cas 有一个或多个图像。

所以我想获取一个用户的所有对话,并且我能够填充文章,但我无法使用下面的图片填充文章

 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 无法进行深度填充,所以我尝试使用循环来使用关联图像填充每篇文章并将其设置在对话中,但文章从不设置在对话中。

我该如何解决?

【问题讨论】:

    标签: json node.js sails.js


    【解决方案1】:

    从结尾处的if(i==conversations.length) 来看,您似乎有一种暗示,您需要编写异步代码。但是您在同步forEach 循环内迭代i,因此您的响应甚至在任何数据库查询运行之前就发生了。将i++if 移动到Article.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 实例分配给对话之前,您还需要在newArticle 实例上调用toObject,因为它包含images 属性上的getter 和setter,它们在复制时会出现意外行为。

    我还建议将其重构为使用async.each,这将使其更具可读性。

    【讨论】:

    • 您好 sgress454,我已经尝试过您的建议,但结果仍然相同,当前对话中的文章未与 newArticle 更新
    • 另外,你能用 async.each 重构代码更新你的 anwser 吗?
    • 抱歉,我忽略了在 newArticle 实例上添加 toObject 调用。用一些解释更新了代码。至于async.each,正如你所说——这是一个重构。我只是建议它可以使代码更具可读性,但这并不是必须的。
    【解决方案2】:

    直到这个问题解决(https://github.com/balderdashy/sails-mongo/issues/108),你可以使用我开发的这个函数来解决这个问题:https://gist.github.com/dinana/52453ecb00d469bb7f12

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多