W
W
wini6662017-08-26 14:36:44
symfony
wini666, 2017-08-26 14:36:44

How to make .then work inside a Promise?

How to make .then work inside a Promise ? The second find by tags no longer works because searchArr is not ready...

articleSchema.statics.list = function ({tag, sort, limit, skip}) {

    return new Promise(function (resolve, reject) {

        const Tag = mongoose.model('Tag');
        let searchArr = [];

        Tag.find({label: tag}, {label: false, updatedAt: false, createdAt: false, articleId: false, __v: false}).exec()
            .then((findTags) => {
                findTags.map((item, index) => {
                    searchArr.push(String(item._id));
                });
                return searchArr;
            })
            .then((searchArr) => {
                return this
                    .find({
                        'tags': {
                            $in: searchArr

                        }
                    })
                    .sort('-' + sort)
                    .skip(parseInt(skip))
                    .limit(parseInt(limit))
                    .populate('tags')
                    .exec()
                    .then((Articles) => {
                        resolve(Articles);
                        console.log('Articles');
                        console.log(Articles);
                    })
                    //.catch(e => next(e));
            })
            //.catch(e => next(e));

        //resolve(Articles);

    });

};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2019-05-03
@vexgg

If it's an Action, then $id is a query parameter. It is usually passed in the request uri and denoted in the routing template as {:id} .

V
Vladimir, 2017-08-26
@wini666

https://habrahabr.ru/company/mailru/blog/269465/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question