Skip to content

Multiple find with promise #29

@krisnamargono

Description

@krisnamargono

Hello, is it possible to query twice and then save it into redis? I kinda confused on how to explain it. So here is my example code.

var cacheObj = models.cacher(models.sequelize, models.rc)
                .model('model1')
                .ttl(60);

            var allModel1Data = [];

            cacheObj.findAll({}).then(function(model1) {
                var allModel1Data = JSON.parse(JSON.stringify(model1));

                return models.Model2.findAll({})
            }).then(function(model2) {
                for(var i = 0; i < allModel1Data.length; i++) {
                    allModel1Data[i].Model2Array = [];

                    for(var j = 0; j < model2.length; j++) {
                        if(model2[j].Model1Id == allModel1Data[i].Id) {
                            allModel1Data[i].Model2Array.push(model2[j]);
                        }
                    }
                }

                return res.json({
                    success: true,
                    model1: allModel1Data
                })
            })

In the 'allModel1Data', it should be containing all the data combined from Model1 and Model2. But instead of that, it only returns [].
Am I wrong? Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions