में मेरे 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);
से लॉग इन किया है तो मुझे सर्वर से वापस हो रही है त्रुटि संदेश, है:
यह आसानी से हो सकता है कि 'err' या 'err.response' अनिर्धारित है, इसलिए' err.response.data 'को लॉग करने का प्रयास करने से एक त्रुटि फेंक दी जाएगी, इसलिए, आप' .catch' का उपयोग कर रहे हैं - क्योंकि यह फेंकता है एक त्रुटि, आपके हाथों पर एक अनचाहे अस्वीकृति है –
मैंने err.response.data को लॉग किया है - मैंने इसे SO प्रश्न में भी शामिल किया है। यह सर्वर से त्रुटि ठीक से देता है। @JaromandaX –
कोड में आप स्पष्ट रूप से 'त्रुटि प्राप्त करते समय बड़ी समस्याओं में भाग लें' - आप कभी नहीं कहते कि 'console.log (' ... ', err.response.data)' आउटपुट - आप त्रुटि संदेश दिखाते हैं आप सर्वर से प्राप्त करते हैं, लेकिन यह 'err.response.data' नहीं हो सकता है - ऐसा लगता है कि यह सिर्फ 'err.response.data' –