7

की संपत्ति 'नाम' नहीं पढ़ सकता है मैंने फ़ॉर्म इनपुट में फेसबुक एपीआई का उपयोग करके डेटा लाने की कोशिश की। अब, जब तक मैं वर्तमान उपयोगकर्ता के स्थान को लाने की कोशिश नहीं करता तब तक सब कुछ ठीक हो गया।Uncaught TypeError: अपरिभाषित

यदि वर्तमान उपयोगकर्ता ने अपना स्थान साझा किया है (जहां वह रह रहा है), तो मुझे कोई समस्या नहीं है। हालांकि, अगर उपयोगकर्ता ने फेसबुक पर अपना स्थान साझा नहीं किया है, तो मुझे एक त्रुटि मिल रही है: Uncaught TypeError: Cannot read property 'name' of undefined

यहां कोड है जिसका उपयोग मैं कर रहा हूं। क्या आपको पता है कि यह कैसे हल करने के लिए है, तो यहां टिप्पणी कृपया :)

<div id="fb-root"></div> 
<script> 
    // Additional JS functions here 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '*******', // App ID 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true // parse XFBML 
    }); 

    FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 

    FB.api('/me', function(response) { 
    document.getElementById("user_login").value="FB - " + response.username; 
    document.getElementById("user_email").value=response.email; 
    document.getElementById("first_name").value=response.first_name; 
    document.getElementById("last_name").value=response.last_name; 
    document.getElementById("user_url").value=response.link; 
    document.getElementById("fbid").value=response.id; 
    if(response.location !== 'undefined') 
    { 
    document.getElementById("location").value=response.location.name; 
    alert(response.location.name); 
    } 
    document.getElementById("registerform").submit(); 

}); 

    } else if (response.status === 'not_authorized') { 
    alert('error'); 
    } else { 
    alert('Please Log In!'); 
    } 
}); 

    }; 

    // Load the SDK Asynchronously 
    (function(d){ 
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
    if (d.getElementById(id)) {return;} 
    js = d.createElement('script'); js.id = id; js.async = true; 
    js.src = "//connect.facebook.net/en_US/all.js"; 
    ref.parentNode.insertBefore(js, ref); 
    }(document)); 
</script> 

उत्तर

16

इस लाइन पर:

if(response.location !== 'undefined') 

... आप अगर response.locationस्ट्रिंग"undefined" नहीं है जाँच कर रहे हैं। चूंकि undefined स्ट्रिंग "undefined" नहीं है, इसलिए स्थिति सत्य है। फिर आप response.location.name का उपयोग करने की कोशिश करते हैं और response.locationundefined है, तो आपको त्रुटि मिलती है।

आप शायद मतलब या तो:

if(response.location) 

या

if(typeof response.location !== 'undefined') 
+0

धन्यवाद! मेरी गलती ... –

+0

@IdoDoron: कोई चिंता नहीं, खुश है कि मदद की! बनाने के लिए आसान गलती। –

संबंधित मुद्दे