You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using a function to open the database and try to use the assigned server variable in other functions:
var server;
function OpenDB() {
db.open({
.....
}).then(function(s) {
server = s;
});
}
function DoSomethingInDB() {
console.log("Server var:", server); <<<< undefined
server.mytable.count().then(function(ct) { <<<error: Cannot read properties of undefined (reading 'mytable')
...
});
}
....
OpenDB();
DoSomethingInDB();
My database is successfully created in OpenDB().
Unfortunately, it seems that the global server var becomes undefined outside the OpenDB function.
From documentation:
A connection is intended to be persisted, and you can perform multiple operations while it's kept open
But I didn't close it. Bah?
For sure I'm doing something wrong. Can you help me?
Thanks.
The text was updated successfully, but these errors were encountered:
radiolondra
changed the title
Global Server variable became undefined?
Global 'server' variable became undefined?
Dec 27, 2022
radiolondra
changed the title
Global 'server' variable became undefined?
Global 'server' variable becomes undefined?
Dec 27, 2022
Hey, I do not think this is an issue. Your then() is likely executing afterDoSomethingInDB() so server is not even assigned to yet. To avoid this, in an async function, you need to await db.open().
I'm using a function to open the database and try to use the assigned
server
variable in other functions:My database is successfully created in
OpenDB()
.Unfortunately, it seems that the global
server
var becomes undefined outside theOpenDB
function.From documentation:
But I didn't close it. Bah?
For sure I'm doing something wrong. Can you help me?
Thanks.
The text was updated successfully, but these errors were encountered: