Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sails MASTER --> Waterline Error #3014

Closed
Oldwo1f opened this issue Jun 14, 2015 · 9 comments
Closed

Sails MASTER --> Waterline Error #3014

Oldwo1f opened this issue Jun 14, 2015 · 9 comments

Comments

@Oldwo1f
Copy link

Oldwo1f commented Jun 14, 2015

Leaving this issue #3008, i updated my sails apps to master branch to benefit of the fix from @sgress454 .

But something else in the master branch make my application crazy.
All my complexe method to fetch items, add/delete/update items with association doesnt work anymore.

Error thrown mention 'collection' and waterline

TypeError: Cannot read property 'collections' of undefined
    at /media/terra1/www/hours/node_modules/sails/node_modules/waterline/lib/waterline/utils/nestedOperations/reduceAssociations.js:50:33

Here is one sample route where i this error occure. (It just fetching a article with his associated collection.) I am 100% sure this worked on sails 0.11.0

article:function(req,res,next) {
        req.locale = req.locale || 'en'
        moment.locale(req.locale)
                Article.find(req.params.id).populateAll().exec(function (err,items){
                                           if(err)
                            callback(err)
                        if(items.length>0)
                        {
                                items[0].nbView= Number(items[0].nbView) + 1;
                                items[0].save();
                                var project= items[0];
                                // console.log('item',item);
                                async.series({
                                image:function(cbparalelle) {
                                    async.map(project.images,
                                    function(item1,cb1) {
                                        // console.log('item1',item1);
                                        Image.findOne(item1.image).exec(function(err,data) {
                                            item1.image=data
                                            cb1(null,item1)
                                        })

                                    },function(err, results) {
                                        // console.log('results',results);
                                        cbparalelle(null,results)
                                    })
                                },
                                document:function(cbparalelle) {
                                    async.map(project.documents,
                                    function(item1,cb1) {
                                        // console.log('item1',item1);
                                        Document.findOne(item1.document).exec(function(err,data) {
                                            item1.document=data
                                            cb1(null,item1)
                                        })

                                    },function(err, results) {
                                        // console.log('results',results);
                                        cbparalelle(null,results)
                                    })
                                },
                                translations:function(cbparalelle) {
                                    async.map(project.translations,
                                    function(trad,cb1) {
                                        console.log(trad);
                                        if(trad.lang == req.locale){
                                            console.log('locale');
                                            project.title = (trad.title) ? trad.title : project.title;
                                            project.content = (trad.content) ? trad.content : project.content;
                                            project.rewriteurl = (trad.rewriteurl) ? trad.rewriteurl : project.rewriteurl;
                                            project.keyword = (trad.keyword) ? trad.keyword : project.keyword;
                                            project.description = (trad.description) ? trad.description : project.description;
                                        }
                                        cb1(null,project)

                                    },function(err, results) {
                                        // console.log('results',results);
                                        cbparalelle(null,results)
                                    })
                                },
                                comment:function(cbparalelle) {
                                            console.log('------------------------------');
                                            // console.log(project.comments);
                                            _.remove(project.comments,function (n) {
                                                return n.status != 'success'
                                            })
                                            var allcomments = [];
                                        // _.sortBy(project.comments,function (n) {
                                        //  return new Date(n.createdAt)
                                        // })
                                            project.comments = project.comments.reverse()

                                    async.mapSeries(project.comments,
                                    function(item1,cb1) {
                                        // console.log('item1',item1);
                                        Comment.find(item1.id).populate('reponses',{ where:{status:'success'}}).exec(function(err,data) {
                                            // item1.comment=data
                                            // console.log(data);
                                            console.log('------------------------------');
                                            // console.log(project.comments.indexOf(item1));
                                            // item1=data
                                            // project.comments.splice(project.comments.indexOf(item1),1,data[0])
                                            allcomments.push(data[0])
                                            // console.log(data);
                                            cb1(null,item1)
                                        })

                                    },function(err, results) {

                                        project.comments=allcomments;
                                        // console.log('allcomments',allcomments);
                                        cbparalelle(null,allcomments)
                                    })
                                }
                            },function(err,ress) {

                                        console.log('DELETE');
                                    if(project.category)
                                        project.category=project.category.id;


                                    var projecttogo = _.clone(project)

                                    delete projecttogo.comments
                                    projecttogo.comments=ress.comment
                                    var pathtoshare ='';
                                    pathtoshare = sails.config.URL_HOME +'article/'+ projecttogo.id +'/';
                                    if(projecttogo.urlrewrite)
                                    pathtoshare = sails.config.URL_HOME +'article/'+ projecttogo.id +'/'+projecttogo.urlrewrite;


                                    res.status(200).view('article',{
                                        'article':projecttogo,
                                        moment: moment,
                                        pathtoshare:pathtoshare,
                                        'title': projecttogo.title +' - BLOG - AAVO',
                                        keyword: projecttogo.keyword,
                                        description:projecttogo.description,
                                        scripturl:'article.js',
                                        menu:'blog',
                                        marked:marked
                                    })
                                })
                            }
                });   
}

I really need the fix about socket, i can t downgrade to sails 0.11.0 almost nothing work in my app under master branch.

@sgress454
Copy link
Member

To make a really good test, I would:

  1. Change the Sails dependency in your app's package.json to git://github.com/balderdashy/sails.git
  2. rm -rf node_modules
  3. npm cache clean
  4. npm install

This will guarantee that all of your app's dependencies (including those within the Sails install) are the correct versions to work with the master branch. Give that a shot and let us know if there's any change.

@Oldwo1f
Copy link
Author

Oldwo1f commented Jun 14, 2015

i did exactly this

@sgress454
Copy link
Member

There's a lot going on in that code sample to test. If you can reduce it down to the smallest failing case (maybe just Article.find(req.params.id).populateAll().exec()?) and post the relevant models, that would be better. Or if there's a link to a repo we can test (or if it's private, you can make me a collaborator).

@Oldwo1f
Copy link
Author

Oldwo1f commented Jun 14, 2015

I do not have a repo online yet. I ll try to reduce the bug. I let you know.

@Oldwo1f
Copy link
Author

Oldwo1f commented Jun 14, 2015

I found exactly where the error occure

This is the start of the full method above

article:function(req,res,next) {
        req.locale = req.locale || 'en'
        moment.locale(req.locale)
                Article.find(req.params.id).populateAll().exec(function (err,items){
                                           if(err)
                            callback(err)
                        if(items.length>0)
                        {
                                items[0].nbView= Number(items[0].nbView) + 1;
                                items[0].save(); // <<<-------------- HERE HERE HERE

If i remove this save() the rest of method seems to work correctly.

any idea? Why this save method doesnt work anymore?

I paste you the full error. i case an idea come out

media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/utils/nestedOperations/reduceAssociations.js:50
    var related = self.waterline.collections[attribute.references];
                                ^
TypeError: Cannot read property 'collections' of undefined
    at /media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/utils/nestedOperations/reduceAssociations.js:50:33
    at Array.forEach (native)
    at Object.module.exports [as reduceAssociations] (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/utils/nestedOperations/reduceAssociations.js:25:23)
    at /media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/associationMethods/update.js:65:36
    at Array.forEach (native)
    at new module.exports (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/associationMethods/update.js:45:8)
    at Array.async.auto.updateRecord [as 1] (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:73:41)
    at /media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:640:38
    at _each (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:57:9)
    at Object.async.auto (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:600:9)
    at new module.exports (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:42:9)
    at prototypeFns.save (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/model/index.js:34:14)
    at /media/terra1/www/AAVO/api/controllers/frontController.js:517:18
    at wrapper (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/node_modules/lodash/index.js:3602:19)
    at applyInOriginalCtx (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/media/terra1/www/AAVO/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:320:18)

@Oldwo1f
Copy link
Author

Oldwo1f commented Jun 14, 2015

I add my Article Model

/**
* Article.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

    schema: true,
    attributes: {
        lang : {type:'string',defaultsTo:'fr'},
        title : {type:'string',required:true},
      content : {type:'text',defaultsTo:null},
      shortcontent : {type:'text',defaultsTo:null},
      description : {type:'text',defaultTso:null},
      rewriteurl : {type:'string',defaultsTo:null},
      keyword : {type:'string',defaultsTo:null},
        date : {type:'datetime',required:true},
        nbView : {type:'int',defaultsTo:0},
        status : {type:'string',required:true},
        tags:{collection:'tag'},
      publishVideo:{type:'boolean',defaultsTo:false},
        activeComent:{type:'boolean',defaultsTo:true},
        video:{type:'text',defaultsTo:null},
        category: {
            model: 'categoryBlog'
        },
        author: {
                model: 'user'
            },
        images: {
            collection: 'imagearticle',
            via:'article'
        },
        documents: {
            collection: 'documentarticle',
            via:'article'
        },
        comments: {
          collection: 'comment',
          via:'article'
        },
        translations: {
          collection: 'articletraduction',
          via:'article'
        }
    },
    beforeDestroy: function (values, cb) {
      Article.findOne(values.where.id).populateAll().exec(function(err,item) {
        async.parallel({
          comment:function(callb) {
              async.map(item.comments,
              function(rep, callback){
                      if(rep){
                        Comment.destroy(rep.id,function(err,data) {
                          callback(null)
                        })
                      }
                      else{
                        callback(null)
                      }
              },
              function(err, results){
                if (err) {
                  console.log('ERRRRRRRROR');
                  sails.log(err)
                  return callb(err)
                }else
                {
                  return callb(null)
                }
              });
          },
          imagearticle:function(callb) {
              async.map(item.images,
              function(rep, callback){
                      if(rep){
                        Imagearticle.destroy(rep.id,function(err,data) {
                          console.log('DESTROY Img',data);
                          callback(null)
                        })
                      }
                      else{
                        callback(null)
                      }
              },
              function(err, results){
                if (err) {
                  console.log('ERRRRRRRROR');
                  sails.log(err)
                  return callb(err)
                }else
                {
                  return callb(null)
                }
              });
          },
          documentarticle:function(callb) {
              async.map(item.documents,
              function(rep, callback){
                      if(rep){
                        Documentarticle.destroy(rep.id,function(err,data) {
                          callback(null)
                        })
                      }
                      else{
                        callback(null)
                      }
              },
              function(err, results){
                if (err) {
                  console.log('ERRRRRRRROR');
                  sails.log(err)
                  return callb(err)
                }else
                {
                  return callb(null)
                }
              });
          },
          tags:function(callb) {
              async.map(item.tags,
              function(rep, callback){
                      if(rep){
                        Tag.findOne(rep.id,function(err,data) {
                          if(data){
                            data.nbArticles=Number(data.nbArticles)-1
                            data.save(function() {
                              callback(null)
                            })
                          }
                        })
                      }
                      else{
                        callback(null)
                      }
              },
              function(err, results){
                if (err) {
                  console.log('ERRRRRRRROR');
                  sails.log(err)
                  return callb(err)
                }else
                {
                  return callb(null)
                }
              });
          },
          trads:function(callb) {
              async.map(item.translations,
              function(rep, callback){
                      if(rep){
                        ArticleTraduction.destroy(rep.id,function(err,data) {
                          callback(null)
                        })
                      }
                      else{
                        callback(null)
                      }
              },
              function(err, results){
                if (err) {
                  console.log('ERRRRRRRROR');
                  sails.log(err)
                  return callb(err)
                }else
                {
                  return callb(null)
                }
              });
          },
          cat:function(callb) {

                      if(item.category){
                        console.log(item.category);
                        CategoryBlog.findOne(item.category.id).exec(function(err,data) {
                          console.log(data);
                          data.nbArticles= data.nbArticles-1;
                          data.save().then(function (err,res) {
                             callb(null)
                          })
                        })
                      }
                      else{
                        callb(null)
                      }

          }
        },function(err,results) {
          return cb();
        })


      });
    }
};

@sgress454
Copy link
Member

One issue with your code is that you're treating .save() like a synchronous function, which it is not. The rest of the code underneath it should be called inside the callback of .save:

items[0].save(function(err, savedProject) {...rest of code...});

The rest of the code can be exactly the same (including var project= items[0];, since savedProject won't necessarily be populated). You could also use findOne instead of find at the top to avoid the items[0] dereference.

If changing your .save() call doesn't change anything, I'd try changing it to an .update instead and see if that makes a difference:

Article.update({id: items[0].id}, {nbView: Number(items[0].nbView) + 1})
.exec(function(err, updatedProject){
   ...rest of code...
});

In general, if you're not adding/removing from a many-to-many relationship on a model, .update() is a safer way to go as it guarantees you're not changing properties you don't intend to.

@Oldwo1f
Copy link
Author

Oldwo1f commented Jun 15, 2015

I can t test it right now but i ll let you know.

but why this 'fail' safe method worked on 0.11.0?

@Oldwo1f
Copy link
Author

Oldwo1f commented Jun 15, 2015

Ok, i got this working !

But one other error occur whilch doesnt appear before updating.

i opened a new issue #3015

Maybe you can close this

@Oldwo1f Oldwo1f closed this as completed Jun 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants