I think that the asynchronous functions in the database module of APP lab are really glitchy, or they are very limited in use. I cannot seem to save a value INSIDE the createRecord function to a global variable that will have any visibility outside of the createRecord function. All I wish to do is ‘capture’ the ID value (autonumber) and save to a global variable which can be accessed later for use in the general OnEvent. I know that asynchronous functions have some issues with loops and such, but this is really unreasonable. Can someone explain a work around OR what is going on here?
createRecord("Classes2022", recordAdd, function(record) {
var realID = 0;
studentId = "record " + record.id + " firstname: " + record.firstName +
' lastname: '+ record.lastName + ' Class: ' + record.className;
appendItem(classChars, record.lastName);
console.log(studentId);
realID = record.length;
getRecordId(realID);
});
What I did for my app [WUT] World is define the user’s ID as 0, and then update it when they logged in or signed up.
var userid = 0;
// Sign up code
createRecord("Users", {username: "wutadam", etc: "etc"}, function (rec) {
userid = rec.id;
});
// Log in code
readRecords("Users", {username: "inputtedname", etc: "etc"}, function (rec) {
userid = rec[0].id;
});
It’s not glitchy if you know how to use it right.
There are also synchronous versions of the CRUD functions, which are used like this:
var x = readRecordsSync("Users");
The data tables have four functions called CRUD: Create, Read, Update, and Delete. To get the data in a table, use readRecords
. To update or delete an existing row, use updateRecord
and deleteRecord
.
Hey wutadam,
I sure appreciate the reply. I am trying to ‘simulate’ a relational database with a virtual ‘join’ for the two tables with nonredundant rows in the basic table with students names. The second table will pretty much have the same studentID extracted from the id of the original table (with only first , last names and of course id) but will join as a ONE TO MANY relationship. I cannot think of any other way to try to achieve this since it is evident that the global variable will have no visibility outside of the readRecords() or createRecords functions. I tried your code and certainly learned a trick or two, but the assigned value to userid will still not persist outside of the scope of the readRecord() function. I will really have to go the approach of a more robust text-editor and server-side commands and maybe MYSQL to implement this, I am sure. I may investigate the CRUD operations with readRecordsSync; I still really cannot see how to possible ‘connect’ information for the two tables by extracting information from both tables to store in lists (or objects). The example was really helpful though. I modified your code with some conditional statements, gettext commands, etc. If you have any ideas about this, I would greatly appreciate it. The database feature in CODE.org is a good introduction but I am sure that the ‘pared down’ design for APP lab was not designed to serious try table joins and the like, which I can fully appreciate. I have worked with some asynchronous functions and Node.js, but still very much in the learning stage there.
To use data from a CRUD function outside of the function, define it outside of its scope, like at the top of the code. Then, inside the function, set the variable to the result.
var result = [];
readRecords("Data", {}, function (rec) {
result = rec;
});
That is somewhat like what I did (the variable was defined globally at the beginning of the code) but I went through a loop to generate list (which was defined outside of the readRecords.) I will try similar syntax that you suggest. I just realized something! I guess the console.log() statement is executing FIRST before the asynchronous function populates new list and thus SHOWS no values. I think that must be the issue! Very helpful now. I am guessing that I can work with the data. Thanks for your help; I just really needed to reflect on asynchronous code!