javascript - Uncaught TypeError: Cannot read property 'total' of undefined -
i json response object do:
var json = json.parse(res.text);
i print json , json back. when retrieve value inside json.body.value.total
gives error:
uncaught typeerror: cannot read property 'total' of undefined
i have no idea why. pasted value receive var json , printed on console , able retrieve total. cannot through code. there json value total. unable recognize. on console, works not work in code.
i json response object retrieve using response.text
. think needs change in parsable object returns undefined
it('returns http 200', function (done) { chai .request(baseurl) .get('/api/') .set('authorization', 'basic abc') .query({val:'hey'}) .end(function(err, res) { expect(res).to.have.status(200); var json = res.text; console.log('val: '+ json.parse(json.body)); var val = json.body.value.total; //undefined expect(val.to.be.above(0)); //fails done(); }); });
the rest api
built returning response.body
worked this:
var body = json.parse(json.body); var obj = json.stringify(body); var jsonobj = json.parse(obj);
the above looks ridiculous thats worked. json -> object -> json
. finding trouble figure out json object.
the console doing job not library using.
the complete code this:
it('returns http 200', function (done) { chai .request(baseurl) .get('/api/') .set('authorization', 'basic abc') .query({val:'hey'}) .end(function(err, res) { expect(res).to.have.status(200); var json = res.text; var body = json.parse(json.body); var obj = json.stringify(body); var jsonobj = json.parse(obj); var val = jsonobj.body.value.total; expect(val.to.be.above(0)); done(); }); });
Comments
Post a Comment