Hey everyone,
I am trying to call a playcanvas function from an anonymous async
functiojn in my code, but I cannot seem to get it to work. The code is as follows.
Rating.prototype.rate = function(letter) {
alert(this.entity.name);
};
const predict = async function(arr) {
let inputCalc = tf.tensor(arr);
let data = inputCalc.reshape([1, 8]);
const model = await tf.loadLayersModel('https://raw.githubusercontent.com/ABcoder29/special-tribble/main/model.json');
let tensorOutput = model.predict(data);
let finalRes = parseFloat(tensorOutput.dataSync()[0]);
if (finalRes < 8) {
Rating.prototype.rate("E");
}
else if (finalRes >= 8 && finalRes <= 10) {
Rating.prototype.rate("E");
}
else if (finalRes > 10 && finalRes <= 12) {
Rating.prototype.rate("E");
}
else if (finalRes > 12 && finalRes <= 14) {
Rating.prototype.rate("E");
}
else {
Rating.prototype.rate("E");
}
}
predict
is getting called correctly, as if I change the alert in the rate()
function to a simple string, it works. However, I cannot access the this
keyword inside rate. This doesn’t change even if I use pc.Application.getApplication()
. The error I get is -
Uncaught (in promise) TypeError: Cannot read property 'name' of undefined
at ScriptType.Rating.rate (rating.js?id=37158208&branchId=5bd3b56e-5a78-477a-a019-68fe536f0036:86)
at predict (rating.js?id=37158208&branchId=5bd3b56e-5a78-477a-a019-68fe536f0036:110)
How do I fix this?