在Node + Express Router中使用对象数组时出现问题

时间:2019-01-08 10:28:36

标签: javascript node.js express

我有一条查询路线,可以查询所有类型的对象。如果找到,它们将查询传递属性(FK)的其他两个类。

我将获取结果并将其添加到singleObj1中,然后将它们推入元素数组中。

当我开始将元素添加到allObjs数组中时,它会一直很好直到行“ allObjs.push(singleObj1)”(如果我console.log(JSON.stringfy(allObjs),它将显示数组元素) ,但是当我尝试在代码末尾访问数组时,该数组为空...

我的模特:

Obj3:{ 身份证号码, nameSpace:字符串, 目的地:字符串 }

Obj2:{ 身份证号码, 登录:字符串, 描述:字符串 }

Obj1:{ 身份证号码, Obj1_id:数字, Obj2_id:数字, classType:字符串, creationDate:日期 }

我已经尝试过更改范围,代码在数组中的位置,但是没有任何效果,当我尝试将其发送回API调用程序时,它仍然为空...

var express = require('express');
var router = express.Router();
var Obj3 = require('../models/Obj3s');
var Obj2 = require('../models/Obj2s');
var Obj1 = require('../models/Obj1s');


router.get("/", function(req, res){
    var allObjs = [];
    Obj1.find({}, function(err, foundObj1s){
        if(err){
            res.status(400).send();
        }
        else if (foundObj1s.length > 0 || foundObj1s !== undefined){
            foundObj1s.forEach(function(e){
                 var singleObj1 = {
                    id: Number,
                    classType: String,
                    Obj2:{
                        id: Number,
                        logon: String,
                        description: String
                    },
                    Obj3: {
                        id: Number,
                        nameSpace: String,
                        destination: String
                    },
                    creationDate: Date
                };
                Obj2.findOne({id: e.Obj2_id}, function(err, foundObj2){
                    if(err){
                        res.status(400).send();
                    }
                    if(foundObj2.length > 0 || foundObj2 !== undefined){
                        singleObj1.Obj2.id = foundObj2.id;
                        singleObj1.Obj2.logon = foundObj2.logon;
                        singleObj1.Obj2.description = foundObj2.description;

                        Obj3.findOne({id: e.Obj3_id}, function(err, foundObj3){
                            if(err){
                                res.status(400).send();
                            }
                            if(foundObj3.length > 0 || foundObj3 !== undefined){
                                singleObj1.Obj3.id = foundObj3.id;
                                singleObj1.Obj3.nameSpace = foundObj3.nameSpace;
                                singleObj1.Obj3.destination = foundObj3.destination;

                                singleObj1.id = e.id;
                                singleObj1.classType = e.classType;
                                singleObj1.creationDate = e.creationDate;
                                allObjs.push(singleObj1);
                            }
                        });
                    }
                });
            });
            //allObjs.sort((a, b) => a.id < b.id ? -1 : 1);
            console.log("all objs" + JSON.stringify(allObjs));
            res.status(200).send(JSON.stringify(allObjs));
        }
        else{
            res.status(400).send();
        }
    });

});

最终结果是包含所有Obj1:{Obj2,Obj3}的JSON列表。

0 个答案:

没有答案