仅返回对象的第一个元素

时间:2018-11-25 02:29:16

标签: javascript arrays node.js mongoose

所以我在router.js文件中有这串链接函数,该字符串应该传递一些查询的结果。不幸的是,当我从最后一个链接函数传递数组时,它仅传递了最后一个元素secondDoc。具体在这个地方:

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

代码如下:

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;

我正在使用Mongoose从MongoDB数据库中提取数据,该函数的输出如下:

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 } ] }

我在这里缺少什么东西吗?我如何将firstDoc和secondDoc传递到下一个.then()调用中?

1 个答案:

答案 0 :(得分:0)

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)
    }
})