Javascript - TypeError: x is not a function at done

Multi tool use
Multi tool use


Javascript - TypeError: x is not a function at done



I am building my own browserside javascript sdk, using webpack and node.



I have built a async/await document function, which simply submits a document to an api.



This function could be called twice in the browser (if there is a second image) However on the second document function call i get the following error.



TypeError: hsp.document is not a function
at done



index.html


<script>

hsp = new HSP();

// function called on button click
async function done() {

try {
const doc = await hsp.document(transaction, token, url, this.frontBase64);

console.log(doc);

// If second image, submit it also.
if(this.backBase64) {
const doc = await hsp.document(transaction, token, url, this.backBase64);

}

} catch (er) {
console.log(er);
}

}

</script>



sdk.js


async document(transaction, token, url, doc) {

try {

this.document = await this.api.submitDocument(transaction,
token, url, doc);

if (this.document.response.status !== 200) {

return {
"success": false,
"response": this.document
};

}

return {
"success": true,
"response": this.document
};


} catch (e) {
return {
"success": false,
"response": e
};
}
}





It looks like you're reaffecting the field this.document in your document function., with whatever is returned by the this.api.submitDocument call. So, it's not a function anymore after the first function call.
– Guillaume Georges
5 mins ago



this.document


document


this.api.submitDocument





token, url this.frontBase64); doesn't look like valid syntax
– CertainPerformance
4 mins ago


token, url this.frontBase64);





@GuillaumeGeorges thank you, i forgot i was setting this.document so the function document was not there anymore
– Kay
2 mins ago




1 Answer
1



Apparently, the document function is defined in your HSP object.


document



When calling it a first time, by doing hsp.document, it's executed and inside the function, the this keyword is a reference to the hsp instance you invoked the document from.


hsp.document



Inside your function, you're reaffecting the field this.document in your document function, with whatever is returned by the this.api.submitDocument call. So, it's not a function anymore after the first function call.


this.document


this.api.submitDocument





thanks, this worked. Have to be careful in future when setting this.x to ensure there is no function name called x
– Kay
1 min ago







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

kJk 1n4MXkZV0 imjZMWSSTRN2DJgRd7c
qs6bzN,TAc,ErasSTz,g07kxLJ1ZezIr4acnBrID90CTkAKaS kK7Ryi,Z z9gwn1QxIl09wvdxmNZP ElBb6VXVs L7pe5

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters