0

Why now find() of Article Model don't accept searchArr ? Pls help. very tired, do not think anything Pls help. very tired, do not think anything Pls help. very tired, do not think anything Pls help. very tired, do not think anything Pls help. very tired, do not think anything

articleSchema.statics.list = function ({tag, sort, limit, skip}) {
    const Tag = mongoose.model('Tag');
    const searchArr = [];

    Tag.find({label: tag}, {label: false, updatedAt: false, createdAt: false, articleId: false, __v: false})
        .exec()
        .then( (findTags) =>{

            findTags.map ((item, index) => {

                searchArr.push('' + item._id);
            });
            console.log(searchArr);
        })
        .catch(e => next(e));

    return this
        .find({
            'tags': { $in: searchArr
            }
        })
        .sort('-' + sort)
        .skip(parseInt(skip))
        .limit(parseInt(limit))
        .populate('tags')
        .exec()
        .then( (Articles) =>{
            console.log('Articles');
            console.log(Articles);

        })
        .catch(e => next(e));

};
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
wini666
  • 1
  • 3
  • Javascript adds or fixes your missing ";" ...... "return this (newline)" is not the same as "return this.find ({ ... " You have ran into the issue where a newline is the same as a ; ... that is to say *if a newline can be interrupted as a "; newline" without an error* javascript will interrupt it as "; newline" ... Note "return this. (newline) find" although harder to read should work ... – Wayne Aug 26 '17 at 08:41
  • I voted to reopen this as it is not a scope issue "Why is my variable unaltered" it is a unreachable code which should be given an example not a link to the answer. – Wayne Aug 26 '17 at 08:49
  • a link to @wini666 answer ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Stmt_after_return ... Invalid cases lines 9 and 10. – Wayne Aug 26 '17 at 08:52
  • Note "return this. (newline) find" It is hard to see but there is a period after the this on the return line, that would force the "Automatic Semicolon Insertion" to not insert the semicolon. – Wayne Aug 26 '17 at 08:57
  • "Automatic Semicolon Insertion" only operates when adding a semicolon does not create an error and return this(period) would be an error. – Wayne Aug 26 '17 at 09:00
  • @Wayne Not too sure what you think you are rambling on about here but the "actual problem" is that there are **two** async methods being called, and the code is attempting to fill an external variable within one method and use it in the other. The "correct" thing to do here is to "instead" continue the promise chain, rather than separating them. Much like the linked answers go into more depth explaining. So you are missing the mark everywhere, and wasting a reopen vote where there is a definitive response to this already. – Neil Lunn Aug 26 '17 at 09:12
  • @NeilLunn there is no equivalent outerScopeVar the code is trying to change. There is instead an "Automatic Semicolon Insertion" issue in the return. If that is answered on stackoverflow, likely it is but I did not see it answered, if I had I would only have linked to stackover answer instead of developer.mozilla.org – Wayne Aug 26 '17 at 09:31
  • @NeilLunn Pardon me trying a answer his question in the comments, it did not occur to me to @ you through the comments. – Wayne Aug 26 '17 at 09:33
  • @Wayne No outer scope var? Did you read the question title? `searchArr` being filled from one async function and then is attempted to be referenced in other that does not await completion of the first. I'm actually asking you to tone down the noise and read the linked duplicate yourself. If you don't understand it, then you should be spending time learning instead. – Neil Lunn Aug 26 '17 at 09:37
  • @NeilLunn searchArr is within articleSchema.statics.list = function ( ... what articleSchema.statics.list(); returns is itself, which contains unreachable code. The question itself if poorly written. but the code contains "const searchArr = [];" making searchArr a variable within the function. – Wayne Aug 26 '17 at 09:48
  • @Wayne Please find something else to do. I don't want to listen to these messages all night. The question is on hold and answered as indicated in the duplicate. This is not your own private chat room. – Neil Lunn Aug 26 '17 at 10:44

0 Answers0