Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅返回对象的第一个元素_Javascript_Arrays_Node.js_Mongoose - Fatal编程技术网

Javascript 仅返回对象的第一个元素

Javascript 仅返回对象的第一个元素,javascript,arrays,node.js,mongoose,Javascript,Arrays,Node.js,Mongoose,因此,我在router.js文件中有一系列链式函数,用来传递一些查询的结果。不幸的是,当我从最后一个链式函数传递数组时,它只传递最后一个元素secondDoc。特别是在这个地方: return { Artwork: firstDoc, Support: secondDoc } }

因此,我在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()
调用中?

避免嵌套的then as回调反模式-只需返回承诺,使您的
.then
sflat@CertainPerformance我该怎么做呢?看看每个承诺呼叫链,如果它在
的末尾,那么
return
这是使用async await执行此操作的最佳方法。这就像做梦一样。非常感谢。您能告诉我
lean()
函数的用途吗?lean是一个mongoose函数,它有助于更快地执行查询。
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)
    }
})