updateRecord question


Here is the link to my project: https://studio.code.org/projects/applab/Gs5uKn-c2G0myfzBcsQ2hUg7DaTD3zEliGFhD3oh4yY

My app is designed to get a person’s first name and last name and how much money they want to put into their account. The creation of the data table works fine.
My issue is when I want the data table to update. So, when the user puts in their pin number (example 13), the amount listed for that id is set to an updated amount (amount they have in the table minus the amount lunch costs). For example, if the person with id number 13 has $100 in their account, their new amount is $90 (assuming lunch costs $10).

I’m able to have that new amount show up in the label there but when I try to update the table, the first and last names of my users gets updated with undefined. I only want the amount to be updated in the table and need everything else to stay intact.

Can anyone help?

I hope this question/problem makes sense.

Welcome to the forum! I am looking into this…

Hi @hdupin - I think I understand the problem. Can you download the csv file of the table and upload here, so I can troubleshoot further? When I remix the app, the data table is lost.


Hi, You should be able to create a user by clicking on the button that says Register Student in the top right. Once you enter the first name, last name and amount wanted, this data is created in the table. You will then be assigned an ID. Go back to main screen and put the ID in. You’ll notice that the label shows the correct amount (after deducting lunch cost from balance) and it also updates the amount in the table, but then the first name and last name become undefined.
Hope this makes sense.

Thanks for the info! Unfortunately, when you remix a project, the entire structure of the table is lost (table name, headers, etc.), which is why I need the csv to recreate the table and troubleshoot.

OK, well here’s the data; however, you’ll notice all first and last name records are undefined; they used to be defined but once the updateRecords happens, the record gets changed to undefined.
Well actually looks like, as a new user, I can’t upload anything.
Can you please try to enter a user by clicking on the top right button, enter a person and amount and then try to input the assigned ID.

The App Lab database isn’t like an SQL database. It stores a single JSON object at the id. That means that anything you leave out of the object you update with will not be there anymore.

In the code you wrote you created a whole new object to store into the database. That means anything you didn’t put into the new object from the old object is gone. In your code:

  readRecords("student", {id:pin}, function(records) {
      for (var i =0; i < records.length; i++) {
        console.log(records[i].id + ': ' + records[i].firstName + " has " + (records[i].amount - foodAmount));
        updatedAmount = records[i].amount - foodAmount;
      updateRecord("student", {id:pin, amount:updatedAmount}, function(record, success) {
          if (success) console.log("Record updated with id:" +record.id + "with amount: " + record.amount);
        else write("Record NOT updated");

We see updateRecord("student", {id:pin, amount:updatedAmount},, this creates a new object without any names. So from now on they will be undefined.

Instead look at this:

  readRecords("student", {id:pin}, function(records) {
      console.log(records[0].id + ': ' + records[0].firstName + " has " + (records[0].amount - foodAmount));
      records[0].amount -= foodAmount;
      updateRecord("student", records[0], function(record, success) {
          if (success) console.log("Record updated with id:" +record.id + "with amount: " + record.amount);
        else write("Record NOT updated");

What we see here is that we update the object we got back from the database and then use that same object to update the database. Thus we keep any data that was present, like the name.

I am having the same problem but I don’t understand the purpose of this code (I’m new)
How could I change this code so that it works?

onEvent("nameinput", "change", function( ) { readRecords("users", {id:((parseInt(getText("userkey")) + 935) / 952)}, function() { updateRecord("users", {id:((parseInt(getText("userkey")) + 935) / 952), name:(getText( "nameinput"))}, donothing) }); console.log("nameinput entered text: " + getText("nameinput")); });

Hi @milnedan001,
Would you please create an AppLab program with your attempt at the functionality, then click “share” and provide the link here? That will help us have better context for what your’e working on. When you share the link, please also provide:

  • What you’d like to have happen
  • What you’ve tried in order to make it work