2017-08-31 17 views
5

में मेरे react-native लॉगिन स्क्रीन पर एप्लिकेशन मैं गलत उपयोगकर्ता नाम में प्रवेश करने के बाद उपयोगकर्ता को अच्छी त्रुटि संदेश प्रदान करने पर काम कर रहा हूं/पासवर्ड संयोजन। एपीआई के साथ बातचीत करने के लिए मैं पुस्तकालय एक्सिस का उपयोग कर रहा हूं। हालांकि जब मुझे catch कथन में कोई त्रुटि मिलती है, तो मुझे यह बदसूरत त्रुटि संदेश कहता है कि मेरे पास "अनचाहे वादा अस्वीकृति" है और मैं घटकों की स्थिति सेट करने या किसी नए पृष्ठ पर नेविगेट करने जैसी चीजें नहीं कर सकता।एक्सिस प्रोमिस हैंडलिंग - "संभावित अनचाहे वादा अस्वीकृति - टाइप एरर: नेटवर्क अनुरोध विफल" प्रतिक्रिया-मूल

मैं नहीं देख सकता कि मैं क्या गलत कर रहा हूं, ऐसा लगता है कि मैंने दस्तावेज़ों में उदाहरणों को देखा है।

मेरी फ़ॉर्म जमा समारोह में मेरे पास है:

axios.post('http://192.168.1.11:1337/login', { 
    email: this.state.username, 
    password: this.state.password 
}).then(function (response) { 

    // This stuff all seems to work great 
    console.log("The response we got: ", response); 
    if (response.status == 200) { 
    console.log("Status code equals 200"); 
    Actions.homepage(); 
    } 
}).catch(function (err) { 

    // Run into big problems when I get an error 
    console.log("Got an error logging in, here's the message: ", err); 
}); 

किसी को भी मैं गलत यहाँ क्या कर रहा हूँ देख सकते हैं?

पीएस

"Got an error logging in, here's the message:" 

{ [Error: Request failed with status code 401] 
    config: 
    { transformRequest: { '0': [Function: transformRequest] }, 
    transformResponse: { '0': [Function: transformResponse] }, 
    headers: 
     { Accept: 'application/json, text/plain, */*', 
     'Content-Type': 'application/json;charset=utf-8' }, 
    timeout: 0, 
    xsrfCookieName: 'XSRF-TOKEN', 
    xsrfHeaderName: 'X-XSRF-TOKEN', 
    maxContentLength: -1, 
    validateStatus: [Function: validateStatus], 
    method: 'post', 
    url: 'http://192.168.1.11:1337/login', 
    data: '{"email":"[email protected]","password":"dddddd"}' }, 
    response: 
    { data: { message: 'Invalid password', user: false }, 
    status: 401, 
    statusText: undefined, 
    headers: 
    { map: 
     { connection: [ 'keep-alive' ], 
      date: [ 'Thu, 31 Aug 2017 23:30:21 GMT' ], 
      'x-powered-by': [ 'Sails <sailsjs.org>' ], 
      vary: [ 'X-HTTP-Method-Override' ], 
      'content-length': [ '52' ], 
      'access-control-allow-credentials': [ '' ], 
      'access-control-allow-origin': [ '' ], 
      etag: [ 'W/"34-Ymi4isRxuJ6jE1EIS+AQag"' ], 
      'access-control-allow-methods': [ '' ], 
      'access-control-allow-headers': [ '' ], 
      'access-control-expose-headers': [ '' ], 
      'content-type': [ 'application/json; charset=utf-8' ] } }, 
    config: 
     { transformRequest: { '0': [Function: transformRequest] }, 
     transformResponse: { '0': [Function: transformResponse] }, 
     headers: 
     { Accept: 'application/json, text/plain, */*', 
      'Content-Type': 'application/json;charset=utf-8' }, 
     timeout: 0, 
     xsrfCookieName: 'XSRF-TOKEN', 
     xsrfHeaderName: 'X-XSRF-TOKEN', 
     maxContentLength: -1, 
     validateStatus: [Function: validateStatus], 
      method: 'post', 
      url: 'http://192.168.1.11:1337/login', 
      data: '{"email":"[email protected]","password":"dddddd"}' }, 
     request: 
     { url: 'http://192.168.1.11:1337/login', 
      credentials: 'omit', 
      headers: 
      { map: 
       { accept: [ 'application/json, text/plain, */*' ], 
       'content-type': [ 'application/json;charset=utf-8' ] } }, 
      method: 'POST', 
      mode: null, 
      referrer: null, 
      _bodyInit: '{"email":"[email protected]","password":"dddddd"}', 
      _bodyText: '{"email":"[email protected]","password":"dddddd"}', 
      bodyUsed: true } } } 

यहाँ क्या यह एंड्रॉयड एमुलेटर (एक सैमसंग गैलेक्सी एस 7 नकल) पर की तरह लग रहा का एक स्क्रीनशॉट है: यहाँ जो हो कि console.log("Got an error logging in, here's the message: ", err); से लॉग इन किया है तो मुझे सर्वर से वापस हो रही है त्रुटि संदेश, है:

Yellow error bar showing "Possible Unhandled Promise Rejection..."

+0

यह आसानी से हो सकता है कि 'err' या 'err.response' अनिर्धारित है, इसलिए' err.response.data 'को लॉग करने का प्रयास करने से एक त्रुटि फेंक दी जाएगी, इसलिए, आप' .catch' का उपयोग कर रहे हैं - क्योंकि यह फेंकता है एक त्रुटि, आपके हाथों पर एक अनचाहे अस्वीकृति है –

+0

मैंने err.response.data को लॉग किया है - मैंने इसे SO प्रश्न में भी शामिल किया है। यह सर्वर से त्रुटि ठीक से देता है। @JaromandaX –

+0

कोड में आप स्पष्ट रूप से 'त्रुटि प्राप्त करते समय बड़ी समस्याओं में भाग लें' - आप कभी नहीं कहते कि 'console.log (' ... ', err.response.data)' आउटपुट - आप त्रुटि संदेश दिखाते हैं आप सर्वर से प्राप्त करते हैं, लेकिन यह 'err.response.data' नहीं हो सकता है - ऐसा लगता है कि यह सिर्फ 'err.response.data' –

उत्तर

3

इस स्निपेट में गलत कुछ भी नहीं है।

  • आप एक अनुरोध भेजते हैं।
  • आपको 401 - अनधिकृत प्रतिक्रिया मिलती है।
  • कैच त्रुटि प्राप्त करता है और इसे लॉग करता है।

दिलचस्प हिस्सा यह है कि आपको उस अनचाहे वादे अस्वीकृति त्रुटि क्यों मिलती है। लेकिन यह कहीं और फेंक दिया गया है। तो आपको अधिक कोड प्रदान करने की आवश्यकता है।

संपादित करें: शायद आपको कॉलिंग फ़ंक्शन पर अपने अक्षरों का वादा वापस करना होगा?

+0

कूल। मैं आज रात अधिक कोड/जानकारी प्रदान करूंगा। उम्मीद है कि मैं समस्या की असली जड़ पता लगाता हूं। अन्यथा मैं इसे +150 प्रतिनिधि प्रतियोगिता के अंतिम दिन के उत्तर के रूप में स्वीकार करूंगा। धन्यवाद –

+0

बेशक मुझे प्रतिनिधि प्राप्त करने में खुशी होगी लेकिन मैंने आपकी समस्या को ठीक नहीं किया; ओ) –

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