Student trying to use a function parameter to update a table record

This is the student’s issue:
We were not able to figure out how to input a parameter into the update function

ex.

function update(table, ID, parameter, item){
updateRecord(table, {id : ID , parameter : item }, function(){});
//does not work ^
}

instead it makes a new column in the table called parameter rather than the value parameter is assigned to

@jpverhaeghe

Hi, can you click the “Share” link at the top of the project page and paste it here so we can see the entire program? That will help us to see the full context of what is going on.

Thanks,
Elizabeth

https://studio.code.org/projects/applab/OUwpvxybDn5_73rXQkXtx2UoXMnKMSiv…

Also to reset the column set the column item of the table test to any string and remove the column named column

Hi, it looks like the problem as that the column names are not strings, so the only way to use them is to somehow evaluate the string as if it’s not a string using eval. I can’t see the database that you are using because the database doesn’t get remixed, so I can’t actually try to debug the code, but maybe try this:

//ignores parameter sent in and instead creates new column known as the parameter name, column
function updateError(table, ID, column){
  var stringFunction = "updateRecord(table, {id: \"" + ID + "\", " + column;
  stringFunction += ":'Bob'}, function(record) { ";
  stringFunction += "console.log(record);  });";
  eval(stringFunction);
}

eval will evaluate the string as if it’s not a string.

Elizabeth

1 Like

This works. We had to make one minor change to avoid making the id a string. Here is what we ended up using:

//ignores parameter sent in and instead creates new column known as the parameter name, column
function updateError(table, ID, column){

var stringFunction = "updateRecord(table, {id: " + ID + ", " + column;
stringFunction += ":‘Bob’}, function(record) { ";
stringFunction += “console.log(record); });”;
eval(stringFunction);

}

The problem stemmed from having to send a String down as a parameter to then use as a column name. Is there a way to just evaluate the string? if we did that, we could write something a little more concise like:

function update(table, ID, parameter, item){
updateRecord(table, {id : ID , eval(parameter): item }, function(){});
}

2 Likes