How to append my key, values in a javascript object , key being data recieved from Database?

I am writing an API for an articles app , each article belongs to some category. I want to return an object (categoryObj) which has categories as keys and the articles array as the value of that key ( articles of that category type ) (Here, latest and trend arrays are working fine and giving the desired result)

I want to have an object categoryObj with keys as the names of categories and the values as the array of articles of that category. here are the categories stored as objects in db : categories Also please suggest a good way to achive this. I don’t think I am doing this right. here’s what I’m trying to do link1link2

app.get('/', function(req,res){
var latest = [];
var trend = [];
Article.find({},function(err,foundlatest){
    if(err){
        console.log(err)
    } else {
        
        latest.push(foundlatest);
        // console.log(latest)
    }
}).limit(7).sort({$natural:-1});
Article.find({}, function(err, trendingArticles){
    if(err){
        console.log(err)
    } else {
        trend.push(trendingArticles)
    }
}).limit(5).sort({hits: -1})

//// Creating an object categoryObj with articles  of particular category : 
var categoryObj = {}
Category.find({}, {projection: {category:0}}, (err, foundCategory)=>{
    if(err){
        throw err
    }else{
        console.log("**********RESULTS(************")   
        foundCategory.forEach((category)=>{
            var newarr = []
            console.log("CategoryName:" + category.name)
            var objkey = category.name;
            categoryObj[objkey] = []

            for(let i = 0;i<category.articles.length; i++){
                console.log(category.articles[i])
                Article.findById(category.articles[i], (err, foundArticle)=>{
                    if (err){
                        console.log(err)
                    }
                    else{                       
                        newarr.push(foundArticle)
                    }
                    console.log("Printing new array : " + newarr + "***")
                    
                    categoryObj[objkey].push(newarr) ;
                    console.log(objkey)
                    console.log("value of obj: " +JSON.stringify(categoryObj));
                    
                })
                console.log("value of obj at l st: " +JSON.stringify(categoryObj));

            }
        console.log(" categoryArray outside for loop: "+ JSON.stringify(categoryObj))
        console.log("*********** CA * ")
        });
        
    }
});
Article.find({}, function(err,allArticles){
    if(err){
        console.log(err);
    }else {             
         //console.log(allArticles[0].category)
        // console.log(Comment)
        // res.render("images.ejs",{allArticles:allArticles, titles: titles , currentUser: req.user , latest: latest});
        res.json({ allArticles , latest , trend, categoryObj})
    }
})  
});

and here’s the output I got at terminal on hitting / route : output1 output2

Articles App has started on port 3001
DB Connected...: cluster0-shard-00-02-ktzf1.mongodb.net
**********RESULTS(************
CategoryName:categ633
616c41b937de5f42eb77cff8
value of obj at l st: {"categ633":[]}
616c429737de5f42eb77cffa
value of obj at l st: {"categ633":[]}
 categoryArray outside for loop: {"categ633":[]}
*********** CA * 
CategoryName:categ7
616cf0c95da56c743e79dc92
value of obj at l st: {"categ633":[],"categ7":[]}
 categoryArray outside for loop: {"categ633":[],"categ7":[]}
*********** CA * 
    Printing new array : {
      category: [ 616c41b937de5f42eb77cff7 ],
      hits: 0,
      tag: [],
      comments: [],
      _id: 616c41b937de5f42eb77cff8,
      title: '6 article33',
      description: '5 desc33',
      created: 2021-10-17T15:31:05.962Z,
      __v: 0
    }***
    categ633
    value of obj: {"categ633":[[{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":                    [],"comments":[],"_id":"616c41b937de5f42eb77cff8","title":"6 article33","description":"5 desc33","created":"2021-10-17T15:31:05.962Z","__v":0}]],"categ7":[]}
Printing new array : {
  category: [ 616c41b937de5f42eb77cff7 ],
  hits: 0,
  tag: [],
  comments: [],
  _id: 616c41b937de5f42eb77cff8,
  title: '6 article33',
  description: '5 desc33',
  created: 2021-10-17T15:31:05.962Z,
  __v: 0
},{
  category: [ 616c41b937de5f42eb77cff7 ],
  hits: 0,
  tag: [],
  comments: [],
  _id: 616c429737de5f42eb77cffa,
  title: '[new articles.........................sss',
  description: 'new descriptionnnnnnnnnnnnnn',
  created: 2021-10-17T15:34:47.132Z,
  __v: 0
    }***
    categ633
    value of obj: {"categ633":[[{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":    [],"comments":[],"_id":"616c41b937de5f42eb77cff8","title":"6 article33","description":"5         desc33","created":"2021-10-17T15:31:05.962Z","__v":0},{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c429737de5f42eb77cffa","title":"[new articles.........................sss","description":"new descriptionnnnnnnnnnnnnn","created":"2021-10-17T15:34:47.132Z","__v":0}],[{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c41b937de5f42eb77cff8","title":"6 article33","description":"5 desc33","created":"2021-10-17T15:31:05.962Z","__v":0},{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c429737de5f42eb77cffa","title":"[new articles.........................sss","description":"new descriptionnnnnnnnnnnnnn","created":"2021-10-17T15:34:47.132Z","__v":0}]],"categ7":[]}
Printing new array : {
  category: [ 616cf0c95da56c743e79dc91 ],
  hits: 0,
  tag: [],
  comments: [],
  _id: 616cf0c95da56c743e79dc92,
  title: '[new articles..............7777',
  description: 'new description77777',
  created: 2021-10-18T03:58:01.466Z,
  __v: 0
}***
categ7
value of obj: {"categ633":[[{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c41b937de5f42eb77cff8","title":"6 article33","description":"5 desc33","created":"2021-10-17T15:31:05.962Z","__v":0},{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c429737de5f42eb77cffa","title":"[new articles.........................sss","description":"new descriptionnnnnnnnnnnnnn","created":"2021-10-17T15:34:47.132Z","__v":0}],[{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c41b937de5f42eb77cff8","title":"6 article33","description":"5 desc33","created":"2021-10-17T15:31:05.962Z","__v":0},{"category":["616c41b937de5f42eb77cff7"],"hits":0,"tag":[],"comments":[],"_id":"616c429737de5f42eb77cffa","title":"[new  articles.........................sss","description":"new  descriptionnnnnnnnnnnnnn","created":"2021-10-17T15:34:47.132Z","__v":0}]],"categ7": [[{"category":["616cf0c95da56c743e79dc91"],"hits":0,"tag":[],"comments": [],"_id":"616cf0c95da56c743e79dc92","title":"[new  articles..............7777","description":"new  description77777","created":"2021-10-18T03:58:01.466Z","__v":0}]]}

The data is being pushed in newarr but not in the categoryObj. I think the issue is related to scopes in javascript. Here’s my categorySchema:

var categorySchema = new mongoose.Schema({
name: String,
color: String,
totalArticles: 
    {   
        type:Number,
        default: 0
    },
desc : String,
articles: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Article"
    }
]
});

and articleSchema:

var articleSchema  = new mongoose.Schema({
title: String,
image: String, 
// imgURL: String,                                            //{type: String, default: "https://source.unsplash.com/1600x1080/?news"},
description: String,
// category: String,
category: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Category"
    }
],
hits : {
    type: Number , 
    default : 0 
},
tag: [
    {type: String}
],
created: {type: Date, default: Date.now},
comments: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Comment"
    }
]
});

Answer

The problem here was , I was logging the categoryObj while the request to the database was going on. As it takes time to fetch the data , at that time categObj was empty and logging was done even before the completion of the request. So I needed to use promise callbacks here using .then() so I had to write like this :

app.get('/', function(req,res){

//// Creating an object categoryObj with articles  of particular category : 
var categoryObj = {}
Category.find({}, {projection: {category:0}}, (err, foundCategory)=>{
    if(err){
        throw err
    }else{
        console.log("**********RESULTS(************")   
        foundCategory.forEach((category)=>{
            var newarr = []
            var objkey = category.name;
            categoryObj[objkey] = []
            for(let i = 0;i<category.articles.length; i++){
                console.log(category.articles[i])
                Article.findById(category.articles[i], (err, foundArticle)=>{
                    if (err){
                        console.log(err)
                    }
                    else{                       
                        newarr.push(foundArticle)
                    }
                    console.log("Printing new array : " + newarr + "***")
                    console.log("pushing"  + newarr + "to categoryObj[" + objkey + "]");
                    categoryObj[objkey].push(newarr) ;
                    
                }).then(()=> {
                    console.log("value of obj in callback " + JSON.stringify(categoryObj))
                })

            }
        });
    }
}).then(()=> {
    Article.find({}, function(err,allArticles){
        if(err){
            console.log(err);
        }else {             
            res.json({ allArticles , latest , trend, categoryObj})
        }
    })
})

});