为什么我在构建JSON响应时会收到“未定义”条目?

时间:2017-11-16 08:08:54

标签: node.js

我尝试了以下代码。 POSTMAN和控制台响应是:

  

“undefined [object Object],[object Object],[object Object],[object   对象],[对象对象],[对象对象],[对象对象],[对象   对象],“

我想要正确的JSON输出:

    exports.getIssues = function(req, res) {
        console.log(filename + '>>get Issues>>');
        var response = {
            status: Boolean,
            message: String,
            data: String
        };
        var request = require('request');
        var username =
            const.username;
        var password =
            const.password;
        var options = {
            url: 'https://computenext.atlassian.net/rest/api/2/search?jql=status+%3D+Resolved+ORDER+BY+updated',
            auth: {
                username: username,
                password: password
            }
        };

        request(options, function(error, obj) {
            if (error) {
                response.message = appmsg.DATA_NT_FOUND;
                response.status = false;
                response.data = obj;
                res.send(response);
            } else {
                response.message = appmsg.DATA_FOUND;
                response.status = true;
                response.data = JSON.parse(obj.body);
                //res.send(response);
                var issueKey = response.data.issues;

                // var keyData = issueKey[0].key;
                // console.log(response.data.issues);
                // console.log(keyData);
                var output = [];
                for (var i = 0; i < issueKey.length; i++) {
                    var issue = issueKey[i].key;
                    //var key = [];
                    //key.push(issue);
                    console.log(issue);

                    var respon = {
                        status: Boolean,
                        message: String,
                        data: String
                    };
                    var request = require('request'),
                        username =
                        const.username,
                        password =
                        const.username,
                        url = "https://computenext.atlassian.net/rest/api/2/issue/" + issue,
                        auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

                    //console.log(url);
                    request({
                        url: url,
                        headers: {
                            "Authorization": auth
                        }
                    }, function(err, object) {
                        if (object) {

                            var info = object;
                            output.push(info); // this is not working as ouput is undefined at this    point
                            //var pout = JSON.parse(output);
                            //console.log(info);
                            console.log("==============================================================================");
                            //console.log(output);
                            console.log("******************************************************************************");
                            if (issueKey.length === output.length) {
                                respon.message = appmsg.DATA_FOUND;
                                respon.status = true;
                                respon.data = output;
                                //console.log(output);
                                //res.send(respon);


                                var id = issueKey[0].id;
                                console.log(id);

                                var commitout = [];
                                for (var i = 0; i < issueKey.length; i++) {
                                    var commits = issueKey[i].id;
                                    console.log(commits);



                                    var request = require('request'),
                                        username =
                                        const.username,
                                        password =
                                        const.password,
                                        url = "https://computenext.atlassian.net/rest/dev-status/1.0/issue/detail?issueId=" + commits + "&applicationType=stash&dataType=repository",
                                        auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

                                    //console.log(url);
                                    var test = [];
                                    request({
                                        url: url,
                                        headers: {
                                            "Authorization": auth
                                        }
                                    }, function(err, obj1) {
                                        if (obj1) {
                                            var info1 = obj1;
                                            commitout.push(info1);
                                            var resultdone = [];
                                            if (issueKey.length === commitout.length) {
                                                respon.message = appmsg.DATA_FOUND;
                                                respon.status = true;
                                                respon.data = commitout;
                                                // console.log(commitout);
                                                //var test = merge(output, commitout);
                                                //var text = output.body[0]; 

                                                resultdone.push = output + commitout;
                                                console.log(resultdone);
                                                res.send(resultdone);
                                            }
                                        }
                                    });
                                }

                            }
                        }

                    });

                }
            }

        });

    };

1 个答案:

答案 0 :(得分:0)

你想在这做什么:resultdone.push = output + commitout; 这会连接您的数组,因此[object object]。另外为什么要将它分配给resultdone.push?我想你真的想调用这个方法。

请改为尝试:

resultdone.push(commitout.concat(output ))