-3

i have this code:

exports.calculate = function(req, res, next) {
    statistics.commented_quizes = 0;
    statistics.no_commented = 0;
    Promise.all([
        models.Quiz.count(),
        models.Comment.count(),
        models.Quiz.findAll({
            include: [{
                model: models.Comment
            }]
        })
    ]).then(function(results) {                                 // `results` is an array of [quizes, comments, all]
        statistics.quizes               = results[0];
        statistics.comments             = results[1];
        statistics.average_comments     = (statistics.comments / statistics.quizes).toFixed(2);
        for (index in results[2]) {
            if (results[2][index].Comment.length) {
                statistics.commented_quizes++;
            } else {
                statistics.no_commented++;
            }
        }
    }).then(next, next);
};

This code cannot read length property of an undefined, in this case 'Comment'.

What is wrong? I tried to remove the property, but the results don't work properly, so i need to identify the bug, but i don't get it.

Thanks in advance!

Jota
  • 17
  • 6
  • 1
    What's actually in `results[2][index]`? Log the `results[2]` and check. I wonder isn't that because of case sensitive property (`comment`, not `Comment`). – raina77ow Jul 23 '15 at 19:22
  • results[2][index] is empty! Nothing was created. Comment is the name of the data table. I tried to change it by comment and it doesn't work. Sorry my english is no too good. – Jota Jul 23 '15 at 19:38
  • Ok, what's in `results[2]` then? – raina77ow Jul 23 '15 at 19:38
  • console.log(results); ReferenceError: results is not defined! – Jota Jul 23 '15 at 19:55
  • *Where* do you declare `statistics` and `index`? – Bergi Jul 23 '15 at 19:55
  • 1
    I'm sorry, but that doesn't make any sense. If your code fails on `results[2][index]` line, `results` var _is_ defined there. Check where you put the logging line and try again. – raina77ow Jul 23 '15 at 19:56
  • 1
    possible duplicate of [Why is using “for…in” with array iteration such a bad idea?](http://stackoverflow.com/q/500504/1048572) – Bergi Jul 23 '15 at 19:56
  • var statistics = { quizes: 0, comments: 0, average_comments: 0, no_commented: 0, commented_quizes: 0 }; – Jota Jul 23 '15 at 19:56
  • @raina77ow **results** is logged in the promise function .then(function(results). So here i have an array with all, quizes, comments... what is wrong? Look that the data result is perfectly fine until that point. It show number of quizes, comments... – Jota Jul 23 '15 at 20:14
  • @Jota: In one comment you say that the result data is perfectly fine, in another you say that `results` throws a ReferenceError. Which one now? – Bergi Jul 23 '15 at 22:44
  • 3
    This question really has little to do with promises so the title is pretty misleading. The issue is entirely about what's in `results[2]` and how to iterate it properly to get whatever it is you're looking for. If you post the results of a `console.log(results[2])`, we all can likely help much better. – jfriend00 Jul 24 '15 at 00:38
  • @Bergi, first sorry for my english because is not too good yet. The data result still perfecly fine until that point, just before Comment.length. But when the code try to read length of Comment show an undefined error. Thanks very much!!! – Jota Jul 24 '15 at 08:47
  • @jfriend00 Pardon my ignorance. Could you show me to get that log? Thanks a lot!! – Jota Jul 24 '15 at 08:56
  • Chrome debug log: https://developer.chrome.com/devtools/docs/console or right click, select Inspect Element, click on the console tab. – jfriend00 Jul 24 '15 at 14:05

1 Answers1

0

Answering my question...

I finally found the error. Length is not owned by Comment, but comments.

Sorry for de inconveniences.

exports.calculate = function(req, res, next) {
    statistics.commented_quizes = 0;
    statistics.no_commented = 0;
    Promise.all([
        models.Quiz.count(),
        models.Comment.count(),
        models.Quiz.findAll({
            include: [{
                model: models.Comment
            }]
        })
    ]).then(function(results) {                                 // `results` is an array of [quizes, comments, all]
        statistics.quizes               = results[0];
        statistics.comments             = results[1];
        statistics.average_comments     = (statistics.comments / statistics.quizes).toFixed(2);
        for (var i in results[2]) {
            if (results[2][i].comments.length) {
                statistics.commented_quizes++;
            } else {
                statistics.no_commented++;
            }
        }
    }).then(next, next);
};
Jota
  • 17
  • 6