I have a problem accessing the class properties after I call the updateUIFromDatabase()
function.
Console.log
says "startUpCount: undefined
".
Before that, everything seemed to work (database returns 123
as it should when I call pump1.getValueFromDatabase("startUpCount")
).
有人可以发现我在做什么错吗?
class PumpBasic
{
constructor(_name, _databaseTableName)
{
this.name = _name;
this.databaseTableName = _databaseTableName;
this.control = 0;
this.statusOfPump = 0;
this.feedback = 0;
this.motorPTC = 0;
this.dryRunProtection = 0;
this.workingHours = 0;
this.startUpCount = 0;
this.errorCount = 0;
this.feedbackTime = 0;
this.safeToRestart = 0;
this.unAckedError = 0;
this.timeSchedules = [];
}
getValueFromDatabase(variableNameInDatabase)
{
$.ajax({
url: "databaseValuesEnquiry.php",
method: "GET",
async: false,
data:{getDatabaseVariableValue:1 ,"variableNameInDatabase":variableNameInDatabase,"databaseTableName":this.databaseTableName},
success:function(response)
{
return response;
}
})
}
updateUIFromDatabase()
{
this.control = this.getValueFromDatabase("control");
this.statusOfPump = this.getValueFromDatabase("statusOfPump");
this.feedback = this.getValueFromDatabase("feedback");
this.motorPTC = 0;
this.dryRunProtection = 0;
this.workingHours = 0;
this.startUpCount = this.getValueFromDatabase("startUpCount");
this.errorCount = 0;
this.feedbackTime = 0;
this.safeToRestart = 0;
this.unAckedError = 0;
this.timeSchedules = [];
}
}
pump1 =new PumpBasic("pump1", "pump1settingstry");
pump1.getValueFromDatabase("workingHours");
pump1.updateUIFromDatabase();
console.log("startUpCount: ", pump1.startUpCount);
让我们看一下这段代码:
Here you call
getValueFromDatabase
several times. I never worked directly withjQuery
but this block表示您正在向后端发出请求。好吧,javascript就像快速,就像超级快速。您向后端发送请求,当请求返回已执行的其余代码时。这就是为什么您不确定的原因。
The function
getValueFromDatabase
will return aPromise
(you can read more about it here: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Promise).好吧,由于它返回了一个承诺(将来会发生的事情),因此您需要拨打电话,等待电话解决,然后再执行其余需要做的事情。
在这种情况下,它将类似于:
The
await
keyword is just a keyword to say to the javascript engine: 'Hey man, could you please stop the execution of this code until this promise is resolved?'If you would like to learn more about the
await
keyword you can read it here: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/await我希望这不是宽松的,但我希望你能对诺言有所了解