Created
March 21, 2018 14:58
-
-
Save dapperAuteur/49a11a5644883316c5b8724bceb44db4 to your computer and use it in GitHub Desktop.
App.js method calls method in apiCalls.js ; NOT handling erros properly, Unhandled Rejection (TypeError): Cannot read property '_id' of undefined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// error returned from line 4, but I think it's from line 6 | |
// Unhandled Rejection (TypeError): Cannot read property '_id' of undefined | |
export async function getPalabra(p, pObj) { | |
console.log(p, pObj); | |
return fetch(`${APIURL}${p}${pObj._id}`) | |
.then(resp => { | |
if (!resp.ok) { | |
if (resp.status >= 400 && resp.status < 500) { | |
return resp.json().then(data => { | |
let err = { errorMessage: data.message } | |
throw err; | |
}) | |
} else { | |
let err = { errorMessage: "Please Try Again Later. Server Is NOT Responding." }; | |
throw err; | |
} | |
} | |
return resp.json(); | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async handleLoadPalabra(p, pObj){ | |
let palabra; | |
let params = p.slice(0, -1); | |
console.log(p, pObj); | |
if (pObj.hasOwnProperty("_id")) { | |
console.log(pObj); | |
palabra = await apiCalls.getPalabra(p, pObj); | |
console.log(palabra); | |
} else if (pObj.hasOwnProperty('word')) { | |
let word = pObj.word; | |
console.log(word); | |
let findPalabra = this.state[params].filter(param => param.word === word); | |
findPalabra = findPalabra[0]; | |
console.log(findPalabra); | |
// I added lines 16-21 to handle the issue but I'm looking for a better way to handle it and communicate to the use that the word is not found. | |
// if (findPalabra === undefined) { | |
// let err = { errorMessage: "Word NOT Found!" }; | |
// return; | |
// } else if (findPalabra.hasOwnProperty('_id')){ | |
// palabra = await apiCalls.getPalabra(p, findPalabra); | |
// } | |
palabra = await apiCalls.getPalabra(p, findPalabra); | |
console.log(palabra); | |
} | |
console.log(palabra); | |
switch (params) { | |
case "fourLetterWords": | |
this.setState({ fourLetterWord: palabra }); | |
this.props.history.push('/words/four-letter-word'); | |
break; | |
case "prefixSuffixRoots": | |
this.setState({ prefixSuffixRoot: palabra }); | |
this.props.history.push('/words/prefix-suffix-root'); | |
break; | |
case "users": | |
this.setState({ user0: palabra }); | |
break; | |
case "verbos": | |
this.setState({ verbo: palabra }); | |
this.props.history.push('/words/verbo'); | |
break; | |
default: | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment