0

I have an object that when i print it with console.log without stringify it looks like this:

SuiteStats {
type: 'suite',
start: 2019 - 06 - 04T13: 04: 10.640Z,
_duration: 6262,
uid: 'District1',
cid: '0-0',
title: 'District',
fullTitle: undefined,
tests: [],
hooks: [],
suites:
[SuiteStats {
        type: 'suite',
        start: 2019 - 06 - 04T13: 04: 15.271Z,
        _duration: 1621,
        uid: 'Create District5',
        cid: '0-0',
        title: '@sanity, @sanityUpgraded, @debug: Create District',
        fullTitle: undefined,
        tests: [Array],
        hooks: [],
        suites: [],
        end: 2019 - 06 - 04T13: 04: 16.892Z
    }
],
end: 2019 - 06 - 04T13: 04: 16.902Z

}

now I want to get the title of SuiteStats that is inside suites that is inside SuiteStats (it value is: '@sanity, @sanityUpgraded, @debug: Create District')

console.log(SuiteStats.suites[0].title

does not work, it exits with an error about "TypeError: Cannot read property 'title' of undefined"

I tried other way without success, like:

console.log(SuiteStats.suites[0].SuiteStats.title

so what I am doing wrong, and why this object is different from another object I worked with in the past?

this is how the object looks after stringify:

{
"type": "suite",
"start": "2019-06-04T13:29:25.385Z",
"_duration": 5575,
"uid": "District1",
"cid": "0-0",
"title": "District",
"tests": [],
"hooks": [],
"suites": [{
        "type": "suite",
        "start": "2019-06-04T13:29:29.737Z",
        "_duration": 1220,
        "uid": "Create District5",
        "cid": "0-0",
        "title": "@sanity, @sanityUpgraded, @debug: Create District",
        "tests": [{
                "type": "test",
                "start": "2019-06-04T13:29:29.745Z",
                "_duration": 369,
                "uid": "I am logged in as admin user \"ufedadmin\"7",
                "cid": "0-0",
                "title": "I am logged in as admin user \"ufedadmin\"",
                "output": [],
                "state": "passed",
                "end": "2019-06-04T13:29:30.114Z"
            }, {
                "type": "test",
                "start": "2019-06-04T13:29:30.122Z",
                "_duration": 523,
                "uid": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"8",
                "cid": "0-0",
                "title": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"",
                "output": [],
                "state": "passed",
                "end": "2019-06-04T13:29:30.645Z"
            }, {
                "type": "test",
                "start": "2019-06-04T13:29:30.667Z",
                "_duration": 283,
                "uid": "I expect that district \"Argentina3\" was created9",
                "cid": "0-0",
                "title": "I expect that district \"Argentina3\" was created",
                "output": [],
                "state": "passed",
                "end": "2019-06-04T13:29:30.950Z"
            }
        ],
        "hooks": [],
        "suites": [],
        "end": "2019-06-04T13:29:30.957Z"
    }
],
"end": "2019-06-04T13:29:30.960Z"

}

9
  • Your array looks invalid to me [SuiteStats { ? Are you sure you're not trying to access the stringified version of your data? Commented Jun 4, 2019 at 13:19
  • this JSON looks invalid, can you stringify it and share? Commented Jun 4, 2019 at 13:20
  • I shared the object after stringify in the question itself, thanks. Commented Jun 4, 2019 at 13:22
  • the 'suites' array is empty, did you intend to read from the 'tests' array? Commented Jun 4, 2019 at 13:24
  • @AlonYampolski there is another suites before the empty one that is not empty Commented Jun 4, 2019 at 13:25

2 Answers 2

1

Looks like it's working fine:

var data = {
"type": "suite",
"start": "2019-06-04T13:29:25.385Z",
"_duration": 5575,
"uid": "District1",
"cid": "0-0",
"title": "District",
"tests": [],
"hooks": [],
"suites": [{
        "type": "suite",
        "start": "2019-06-04T13:29:29.737Z",
        "_duration": 1220,
        "uid": "Create District5",
        "cid": "0-0",
        "title": "@sanity, @sanityUpgraded, @debug: Create District",
        "tests": [{
                "type": "test",
                "start": "2019-06-04T13:29:29.745Z",
                "_duration": 369,
                "uid": "I am logged in as admin user \"ufedadmin\"7",
                "cid": "0-0",
                "title": "I am logged in as admin user \"ufedadmin\"",
                "output": [],
                "state": "passed",
                "end": "2019-06-04T13:29:30.114Z"
            }, {
                "type": "test",
                "start": "2019-06-04T13:29:30.122Z",
                "_duration": 523,
                "uid": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"8",
                "cid": "0-0",
                "title": "I create or overwrite District \"Argentina3\" with code \"Argentina3\"",
                "output": [],
                "state": "passed",
                "end": "2019-06-04T13:29:30.645Z"
            }, {
                "type": "test",
                "start": "2019-06-04T13:29:30.667Z",
                "_duration": 283,
                "uid": "I expect that district \"Argentina3\" was created9",
                "cid": "0-0",
                "title": "I expect that district \"Argentina3\" was created",
                "output": [],
                "state": "passed",
                "end": "2019-06-04T13:29:30.950Z"
            }
        ],
        "hooks": [],
        "suites": [],
        "end": "2019-06-04T13:29:30.957Z"
    }
],
"end": "2019-06-04T13:29:30.960Z"
};

console.log(data.suites[0].title);

Sign up to request clarification or add additional context in comments.

1 Comment

yes it works when you copy the string like you did, but it does not work when doing:
0

I think its fine. To troubleshoot your object here is an idea you can use

var object = your_json;
var array  = object.suites;
console.log(array.length); //confirm array !empty

//lets see if we can get all the titles
for(var i=0; i<array.length; i++){
    console.log(array[i].title);
}

//since you explicitly know that this is an array of objects. check keys and values 
for (var key in array) {
    var value = array[key];
    console.log(key, value);
}

//troubleshooting: if you think the value is an array write a recursive function
function my_recursive_function(array){
    for(var key in array) {
        var value = array[key];
        if(Array.isArray(value)){
            my_recursive_function(value)
        }else{
            console.log(key, value);
        }            
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.