timgrindall
timgrindall

Reputation: 139

only first element of object returned

So I've got this string of chained functions in my router.js file that is supposed to pass the results of some queries along. unfortunately, when I pass the array from the last chained function, it only passes the last element secondDoc. Specifically at this spot:

                    return {
                        Artwork: firstDoc,
                        Support: secondDoc
                    }
                }
            }).then( (doc) => {

the code is as follows:

router.js:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/3DArtistsForum');
var ObjectId = mongoose.Types.ObjectId;

var verbose = 2;
var setup = false;

var models = require('../models');


/* GET home page. */
router.get('/', function(req, res, next) {

    models.forums.find({}).then( function (doc) {

        return doc;
    }).then( function(doc) {

        var id = doc[0]._id;
        var id_2 = doc[1]._id;
        var id_3 = doc[2]._id;

        models.subforums.find({parentID: id}, function(err, firstDoc) {

            if (err) {
                console.log(err);
            } else {
                // console.log(firstDoc);
                return firstDoc;
            }
        }).then( firstDoc => {

            models.subforums.find({parentID: id_2}, function(err, secondDoc) {

                if (err) {
                    console.log(err);
                } else {

                    var docArray = {
                        Artwork: firstDoc,
                        Support: secondDoc
                    }
                    if (verbose >= 2) {
                        console.log('before passing: \n');
                        console.log(docArray);
                    }

                    return docArray;
                }
            }).then( (doc) => {

                models.subforums.find({parentID: id_3}, function(err, thirdDoc) {

                    if (err) {
                        console.log(err);
                    } else {
                        if (verbose >= 2) {
                            console.log('after passsing: \n');
                            console.log(doc);
                        }

                        if ('Artwork' in doc) {
                            console.log('\'Artwork\' exists!');
                        } else {
                            console.log('\'Artwork\' does not exist!');
                        }
                        if ('Support' in doc) {
                            console.log('\'Support\' exists!');
                        } else {
                            console.log('\'Support\' does not exist!');
                        }

                        var subforumsDoc = {
                            Artwork: doc.Artwork,
                            Support: doc.Support,
                            Coding: thirdDoc
                        }

                        if (verbose >= 2) {
                            console.log('\nsubforumDoc: \n');
                            console.log(subforumsDoc);
                        } 

                        res.render('index', { title: '3D Artist Forum', forums: subforumsDoc});
                    }
                });
            }) 
        });
    })
});

router.get('/subforum/:forumIndex/:subforumIndex', function (req, res, next) {
    var forumIndex = req.params.forumIndex;
    var subforumIndex = req.params.subforumIndex;

    models.forums.find({}).then( function (forumDoc) {

        return forumDoc;
    }).then( forumDoc => {

        models.subforums.find({}).then( function (subforumDoc) {

            var forumName = forumDoc[forumIndex].name;
            var subforumName = subforumDoc[subforumIndex].name;

            if (verbose >= 1) {
                console.log("forumName: " + forumName);
                console.log("subforumName: " + subforumName);
            }

            res.render('subforum', {title: subforumName});
        });
    })
})

module.exports = router;

I'm pulling the data from a MongoDB database using Mongoose and the output of this function is the following:

before passing:

{ Artwork:
   [ { _id: 5bf9e49c132cfe4e1c3edde2,
       name: 'Forum Gallery',
       date: 2018-11-24T23:54:04.632Z,
       description: 'Your best artworks',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3edde6,
       name: 'Focused Critiques',
       date: 2018-11-24T23:54:04.633Z,
       description: 'Where you can get focused critiques',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3edde8,
       name: 'Sketchbooks',
       date: 2018-11-24T23:54:04.633Z,
       description: 'A place to put your personal sketchbooks',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3edde4,
       name: 'Finished Projects',
       date: 2018-11-24T23:54:04.633Z,
       description: 'All your finished projects',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddea,
       name: 'Animations',
       date: 2018-11-24T23:54:04.634Z,
       description: 'All your animation projects',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddec,
       name: 'Blender Tests',
       date: 2018-11-24T23:54:04.634Z,
       description: 'All your experiments and tests',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddee,
       name: 'Traditional',
       date: 2018-11-24T23:54:04.634Z,
       description: 'Traditional mediums such as pencil and paper',
       parentID: 5bf9e49c132cfe4e1c3edddb,
       forumIndex: 0,
       __v: 0 } ],
  Support:
   [ { _id: 5bf9e49c132cfe4e1c3eddf0,
       name: 'Tutorials, Tips and Tricks',
       date: 2018-11-24T23:54:04.634Z,
       description: 'Tutorials, tips and tricks',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddf2,
       name: 'Basics and Interface',
       date: 2018-11-24T23:54:04.634Z,
       description: 'Q&A for newbies',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddf4,
       name: 'Modeling',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A about modeling',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddf6,
       name: 'Materials and Textures',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A about materials and texturing',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddf8,
       name: 'Particles and Physics Simulations',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A about particles and physics simulations',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddfa,
       name: 'Lighting and Rendering',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A about lighting and rendering',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddfe,
       name: 'Compositing and Post Processing',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A about compositing and post processing',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3ede00,
       name: 'Technical Support',
       date: 2018-11-24T23:54:04.635Z,
       description:
        'Q&A related to hardware, drivers, and your favorite 3D program',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3ede02,
       name: '3D Artists Forum Support',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A related to this forum',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 },
     { _id: 5bf9e49c132cfe4e1c3eddfc,
       name: 'Animation and Rigging',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Q&A about animation and rigging',
       parentID: 5bf9e49c132cfe4e1c3edddc,
       forumIndex: 1,
       __v: 0 } ] }
after passsing:

[ { _id: 5bf9e49c132cfe4e1c3eddf0,
    name: 'Tutorials, Tips and Tricks',
    date: 2018-11-24T23:54:04.634Z,
    description: 'Tutorials, tips and tricks',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddf2,
    name: 'Basics and Interface',
    date: 2018-11-24T23:54:04.634Z,
    description: 'Q&A for newbies',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddf4,
    name: 'Modeling',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A about modeling',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddf6,
    name: 'Materials and Textures',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A about materials and texturing',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddf8,
    name: 'Particles and Physics Simulations',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A about particles and physics simulations',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddfa,
    name: 'Lighting and Rendering',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A about lighting and rendering',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddfe,
    name: 'Compositing and Post Processing',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A about compositing and post processing',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3ede00,
    name: 'Technical Support',
    date: 2018-11-24T23:54:04.635Z,
    description:
     'Q&A related to hardware, drivers, and your favorite 3D program',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3ede02,
    name: '3D Artists Forum Support',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A related to this forum',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 },
  { _id: 5bf9e49c132cfe4e1c3eddfc,
    name: 'Animation and Rigging',
    date: 2018-11-24T23:54:04.635Z,
    description: 'Q&A about animation and rigging',
    parentID: 5bf9e49c132cfe4e1c3edddc,
    forumIndex: 1,
    __v: 0 } ]
'Artwork' does not exist!
'Support' does not exist!

subforumDoc:

{ Artwork: undefined,
  Support: undefined,
  Coding:
   [ { _id: 5bf9e49c132cfe4e1c3ede04,
       name: 'Released Scripts and Themes',
       date: 2018-11-24T23:54:04.635Z,
       description: 'Scripts, addons, & themes',
       parentID: 5bf9e49c132cfe4e1c3edddd,
       forumIndex: 2,
       __v: 0 } ] }

Is there something dumb here I'm missing? How would I pass both firstDoc and secondDoc into the next .then() call?

Upvotes: 0

Views: 277

Answers (1)

Raunik Singh
Raunik Singh

Reputation: 224

router.get('/', async (req, res, next) => {
    try{
            let doc = await models.forums.find({}).lean();
            let firstDoc = await models.subforums.find({parentID: doc[0]._id }).lean();
            let secondDoc = await models.subforums.find({parentID: doc[1]._id }).lean();
            let thirdDoc = await models.subforums.find({parentID: doc[1]._id }).lean();
             var docArray = {  Artwork: firstDoc, Support: secondDoc }
            if (verbose >= 2) {
                console.log('before passing: \n');
                console.log(docArray);
            }
            if (verbose >= 2) {
                            console.log('after passsing: \n');
                            console.log(doc);
                        }

                        if ('Artwork' in doc) {
                            console.log('\'Artwork\' exists!');
                        } else {
                            console.log('\'Artwork\' does not exist!');
                        }
                        if ('Support' in doc) {
                            console.log('\'Support\' exists!');
                        } else {
                            console.log('\'Support\' does not exist!');
                        }

                        var subforumsDoc = {
                            Artwork: doc.Artwork,
                            Support: doc.Support,
                            Coding: thirdDoc
                        }

                        if (verbose >= 2) {
                            console.log('\nsubforumDoc: \n');
                            console.log(subforumsDoc);
                        } 

                        res.render('index', { title: '3D Artist Forum', forums: subforumsDoc});        


    }catch(err){
        return console.log(err)
    }
})

Upvotes: 1

Related Questions