0

I'm fairly new to node & express, I'm trying to implement a register application.

I have 2 models, both models have one common field 'empID'.

const RegisterEntriesSchema = mongoose.Schema({   
    empID: Number,    
    registerType: String,
    registerItemsQuantity: Number,
    registerItemsDesc: String
}, {
    timestamps: true
});

const RegisterEmpSchema = mongoose.Schema({   
    empID: Number,
    empName: String,
    empPhone: String,
    empProj:String
}, {
    timestamps: true
});

For my get call in which I need to merge the values, I get from RegisterEmpSchema with its corresponding employee details from RegisterEmpSchema.

exports.findAllRegisterEntries = (req, res) => {
    registerEntriesModel.find()
        .then(result => {
            var updatedResponse=[];
            console.log(result[0].empID);
            for(var i=0;i<result.length;i++){

                registerEmpModel.find({ empID: result[i].empID })
                .then(result2 => {                   
                  **//unable to access result here**

                }).catch(err => {
                   console.log("exception catch called findAllRegisterEntries, find employee details "+err);
                });
            }
            res.send(updatedResponse);
        }).catch(err => {
            res.status(500).send({
                message: err.message || "Some error occurred while retrieving register."
            });
        });
};

I basically need to get register data and its corresponding employee data.

How do I modify my find() code to use the key empID and do a join query fetch?

usr30911
  • 2,731
  • 7
  • 26
  • 56
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Aritra Chakraborty Dec 05 '19 at 18:36
  • I advice you to embed a model in the other, unless you absolutly need them to be apart – jogarcia Dec 05 '19 at 18:39

1 Answers1

0

I think you better use populate, add ref to empID inside RegisterEntriesSchema

const RegisterEmpSchema = new mongoose.Schema({
    empID: Number,
    empName: String,
    empPhone: String,
    empProj: String
}, {
    timestamps: true
});

const registerEmpModel = mongoose.model('RegisterEmpSchema', RegisterEmpSchema, 'registerEmployeeCollection');

const RegisterEntriesSchema = new mongoose.Schema({
    registerType: String,
    registerItemsQuantity: Number,
    registerItemsDesc: String,
    empID: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'RegisterEmpSchema'
    }
}, {
    timestamps: true
});

RegisterEntriesSchema.index({ createdAt: 1 }, { expires: '525601m' });

const registerEntriesModel = mongoose.model('RegisterEntriesSchema', RegisterEntriesSchema, 'registerEntriesCollection');

module.exports = {
    registerEmpModel, registerEntriesModel,
}

then use populate() to populate the RegisterEntriesSchema with correspondence empID

 RegisterEntriesSchema.
    find().
    populate('empID').
    exec(function (err, data) {
        if (err) return console.log(err);
        res.send(data);
    });

check mongoose docs: https://mongoosejs.com/docs/populate.html

usr30911
  • 2,731
  • 7
  • 26
  • 56
M. khalil
  • 36
  • 5