2016-07-16 2 views
-3

Ich verwende die Instagram-API, um nach einem bestimmten Tag zu suchen und dann das erste Bild des Ergebnisses anzuzeigen. Hier benutze ich die JSON.parse() Funktion parse den JSON, der von der Erstellung einer HTTP-Anfrage an https://api.instagram.com/v1/tags/{tag}/media/recent?access_token={access token} zurückkommt. Hier ist, was die Anfrage mir zurückgibt (- alles was egal ist). Hinweis: Ich habe Teile der URLs ausgeblendet, vorausgesetzt, sie sind korrekt.Instagram-API: JSON.parse() kann die Eigenschaft undefinierten Knoten Js nicht lesen

{ 
    "data":[ 
     { 
     "images":{ 
      "low_resolution":{ 
       "url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", 
       "width":320, 
       "height":320 
      }, 
      "thumbnail":{ 
       "url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", 
       "width":150, 
       "height":150 
      }, 
      "standard_resolution":{ 
       "url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", 
       "width":640, 
       "height":640 
      } 
     } 
     } 
    ] 
} 

Hier ist der Code, den ich um die Anforderung zu machen bin mit und analysieren, um die JSON:

request.get("https://api.instagram.com/v1/tags/" + tag + "/media/recent?access_token=" + accessToken, function(error, response, body) 
           { 
           console.log(JSON.parse(body).data.images.standard_resolution.url) 
           }); 
        }); 

Wann immer ich diesen Code ausführen es mir diesen Fehler gibt:

/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52 
           console.log(JSON.parse(body).data.images.standard_resolution.url) 
                   ^

TypeError: Cannot read property 'images' of undefined 
    at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67) 
    at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22) 
    at emitTwo (events.js:106:13) 
    at Request.emit (events.js:191:7) 
    at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10) 
    at emitOne (events.js:96:13) 
    at Request.emit (events.js:188:7) 
    at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12) 
    at emitNone (events.js:91:20) 
    at IncomingMessage.emit (events.js:185:7) 

I dachte, dass es vielleicht sein könnte, weil console.log() vor JSON.parse() ausgeführt wurde, aber JSON.parse blockiert den Code von nichts anderem ausgeführt wird. Ich überprüfe auch, ob es einen Rückruf oder ein Versprechen hatte, das ich verwenden könnte, aber ich habe nichts gesehen.

Jetzt habe ich keine Ahnung was los ist. Weiß jemand, was diesen Fehler verursachen könnte und wie ich ihn beheben könnte?

EDIT: Okay, so fand ich heraus, dass data ein Datenfeld ist, also muss ich meinen Code ändern data[0] das Problem jetzt sein soll ich folgende Fehlermeldung erhalten, wenn ich den Code ausführen:

/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52 
           console.log(JSON.parse(body).data[0].images.standard_resolution.url) 
                   ^

TypeError: Cannot read property '0' of undefined 
    at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67) 
    at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22) 
    at emitTwo (events.js:106:13) 
    at Request.emit (events.js:191:7) 
    at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10) 
    at emitOne (events.js:96:13) 
    at Request.emit (events.js:188:7) 
    at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12) 
    at emitNone (events.js:91:20) 
    at IncomingMessage.emit (events.js:185:7) 
+0

, da Sie die URLs unbrauchbar machen, vielleicht könnten Sie ein Beispiel für die JSON setzen in Ihrer Frage zurückgegeben werden – Tibrogargan

+0

@Tibrogargan Es liegt direkt unter dem ersten Absatz des Textes bereits – pjtnt11

+1

sorry, verpaßt Das. Daten sind ein Array. Sie möchten wahrscheinlich Daten [0] .images – Tibrogargan

Antwort

0

data ist ein Array, so dass Sie das erste Element ...

JSON.parse(body).data[0].images

siehe folgende Arbeits Schnipsel zugreifen müssen ...

var someObject = '{ \ 
 
    "data":[ \ 
 
     { \ 
 
     "images":{ \ 
 
      "low_resolution":{ \ 
 
       "url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \ 
 
       "width":320, \ 
 
       "height":320 \ 
 
      }, \ 
 
      "thumbnail":{ \ 
 
       "url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \ 
 
       "width":150, \ 
 
       "height":150 \ 
 
      }, \ 
 
      "standard_resolution":{ \ 
 
       "url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \ 
 
       "width":640, \ 
 
       "height":640 \ 
 
      } \ 
 
     } \ 
 
     } \ 
 
    ] \ 
 
}'; 
 

 

 

 
console.log(JSON.parse(someObject).data[0].images.low_resolution.url);

+0

Das funktioniert nicht für mich. Ich habe einen Beitrag zu meiner Post erklärt. – pjtnt11

+0

wahrscheinlich 'Körper' hat nicht, was Sie denken, vielleicht versuchen Sie' Antwort'?Aber wenn JSON gepostet wird, funktioniert das obige ... – WhiteHat

Verwandte Themen