How do I either: change record id of an entry or update a record with data from a field in an entry?


#1

I’m attempting to set up a raspberry Pi with an rc522 RFID reader to track physical items with RFID tags attached to them. The only problem is that node.js and rc522 - npm can only read the permanent RFID serial number at this time. Airtable API only allows the update function to find an existing entry by it’s record id. I can put the RFID serial number in a field for each item, but I am unaware of any way to query that field. So, I need to find a way to change the record ID to the permanent RFID serial number or update without the record id.
What I want to write:

var rc522 = require(“rc522”);
var Airtable = require(‘airtable’);
var base = new Airtable({apiKey: ‘my api key’}).base(‘my app id’);

rc522(function(rfidSerialNumber){
console.log(rfidSerialNumber);

base(‘base name’).replace(’(rfidSerialNumber)’,{
“field name”: “the thing that I’m changing”,
}, {typecast: true}, function (err, record) {
if (err) { console.error(err): return; }
})
});

I know the (rfidSerialNumber) works with the API as a variable because I can put it in as a field and it gets put into the base correctly, but I need it to be the unique id. Ideally, I would just change the record ID to match the serial number of the RFID tag. Maybe that’s really easy? Or I can create a view that has all of the items listed and fetch the record id that has the RFID’s serial in a field associated with it? (like: fetch record id containing “(rfidSerialNumber)”) All I’m trying to do is set up a system to click an item on an rfid scanner to change it’s location to that scanners location. If there’s a scanner in the safe for example, we click an item on that scanner and Airtable updates to say “in safe” in the location field for that item. Any help would be appreciated.


#2

You can search for a given record using a different field other than its primary field by leveraging the filterByFormula parameter. If you already have the RFID tags stored in Airtable (and based on my understanding of your question, you do), then you can search for records that match that RFID tag. This will allow you to grab the record’s ID, which you can then use to update the record with additional data.

  var base_id = "SOME_BASE_ID";
  var base = Airtable.base(base_id);
  var table_name = "MY_TABLE";

  var table = base.table(table_name);
  
  // lookup the RFID number in our table and then update that record
 // with additional data
  table.select({
    "filterByFormula": "{RFID}=53508398"
  }).firstPage((err, results) => {
    if (err) {
      // handle err;
      throw err;
    }

    // results will be a list, but if the column we are searching for should unique, 
    // then we should only have one element in the array
   // if the uniqueness assumption here is not true, then you'll want to turn this into a loop
    var record = results[0];

    // grab the record id
    // and then push an update to this record
    var record_id = record.id;
    table.update(record_id, {
      "Test Column": "LOREM IPSUM"
    }).then((result) => {
      // handle final result
      console.log(result);
    }).catch((err) => {
      // handle error
    })
  });