javascript - 仅返回对象的第一个元素

标签 javascript arrays node.js mongoose

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

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

代码如下:

路由器.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()调用中?

最佳答案

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

关于javascript - 仅返回对象的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53464160/

相关文章:

arrays - 在 Julia 中初始化数组

node.js - 为什么我的快速代码中的下一个函数无法正常工作?

javascript - grails创建js文件并从gsp引用它

当用户使用滚动条时,不会触发 javascript 重定向

javascript - 如何使用 Angular.js/JavaScript 在点击事件中动态检查复选框

arrays - Kotlin 和泛型,用泛型数组实现抽象泛型类

ruby - Array和Enumerable有什么关系?

node.js - Node 0.12.x const 处于严格模式问题

javascript - 在异步函数更新 Node.js 模块后,如何从该模块返回值?

javascript - 如何使用 Node.js 解压文件