In node.js app, In server.js file I am trying to the request and the print the values to console from an external api called Deckofcardsapi using the below code.
const request = require('request');
request('https://deckofcardsapi.com/api/deck/new/draw/?count=2', function (error, response, body) {
const data = JSON.parse(body);
console.log('remaining :', data.remaining);
console.log('statusCode:', response && response.statusCode);
console.log('suit:', data.cards[suit]);
});
when i try data.cards[suit] i get an error as below.
console.log('suit:', data.cards[suit]);
ReferenceError: suit is not defined
at Request._callback (/Users/lokanathc/Projects/deckOfCards/server.js:48:37)
at Request.self.callback (/Users/lokanathc/Projects/deckOfCards/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/Users/lokanathc/Projects/deckOfCards/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (/Users/lokanathc/Projects/deckOfCards/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)`
but when i use just data.cards i get the following output
remaining : 50
statusCode: 200
suit: [ { code: 'QC',
images:
{ png: 'https://deckofcardsapi.com/static/img/QC.png',
svg: 'https://deckofcardsapi.com/static/img/QC.svg' },
value: 'QUEEN',
image: 'https://deckofcardsapi.com/static/img/QC.png',
suit: 'CLUBS' },
{ code: '5S',
images:
{ png: 'https://deckofcardsapi.com/static/img/5S.png',
svg: 'https://deckofcardsapi.com/static/img/5S.svg' },
value: '5',
image: 'https://deckofcardsapi.com/static/img/5S.png',
suit: 'SPADES' } ]
What should i use to access the value of suit: 'SPADES'.
This is the below response from the API .
{
"success": true,
"cards": [
{
"image": "https://deckofcardsapi.com/static/img/KH.png",
"value": "KING",
"suit": "HEARTS",
"code": "KH"
},
{
"image": "https://deckofcardsapi.com/static/img/8C.png",
"value": "8",
"suit": "CLUBS",
"code": "8C"
}
],
"deck_id":"3p40paa87x90",
"remaining": 50
}
suitsis not defined, what is defined however, isdata.cards["suit"]ordata.cards.suitremaining : 50 statusCode: 200 suit: undefined