Help

How can i console.log a filtered record?

Solved
Jump to Solution
289 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Guics
5 - Automation Enthusiast
5 - Automation Enthusiast

 

const table1 = base.getTable("Table 1")

const view = table1.getView("Grid view");

const record_view = await table1.selectRecordsAsync({fields: ["Name"]})

const record_filtered = record_view.records.filter(record => record.name === "Test 1")

 

I'm just trying to understand how scripts work on airtable. i'm not a expert on JS so it can have some errors, but i think i already did the filter in a right way but i can't console.log anything about the "record_filtered"

And im trying to update the record_filtered, but i can't get the recordId

let table = base.getTable("Table 1");
//let record = await input.recordAsync("Converting record", table)

const record_view = await table.selectRecordsAsync({fields: ["Name"]})

const record_filtered = record_view.records.filter(record => record.name === "Test 1")

let test1 = "gui123"
let recordId = record_filtered.id;

await table.updateRecordAsync(recordId, {
    "Name" : test1,
})

 

1 Solution

Accepted Solutions
jsep
7 - App Architect
7 - App Architect

Is because record_view.records.filter returns an array an not object, you have two ways to solve this:

 

// the filter method returns an array, this is good you want to filter more than one record
// returns an array [{...}]
const records_filtered = record_view.records.filter(record => record.name === "Test 1") 

let test1 = "gui123"
let recordId = records_filtered[0].id
//                                ^ access the first item in the array.

await table.updateRecordAsync(recordId, {
    "Name" : test1,
})

 

 Alternatively you can use the find method

 

 

// Another way you can use if you only care about 1 record is the find method
// this method will return the first item that satisfies the provided testing function
// otherwise it will be null
const record_filtered = record_view.records.find(record => record.name === "Test 1") the record {...}

if(record_filtered) {
  let test1 = "gui123"
  let recordId = record_filtered
  // you can use console.log too
  console.log("recordId:", recordId);

  await table.updateRecordAsync(recordId, {
      "Name" : test1,
  })
}

 

 The console.log will look like this:

jsep_0-1712791422690.png

I hope this helps. If you need more help feel free to schedule 15min call with me. 

 

See Solution in Thread

1 Reply 1
jsep
7 - App Architect
7 - App Architect

Is because record_view.records.filter returns an array an not object, you have two ways to solve this:

 

// the filter method returns an array, this is good you want to filter more than one record
// returns an array [{...}]
const records_filtered = record_view.records.filter(record => record.name === "Test 1") 

let test1 = "gui123"
let recordId = records_filtered[0].id
//                                ^ access the first item in the array.

await table.updateRecordAsync(recordId, {
    "Name" : test1,
})

 

 Alternatively you can use the find method

 

 

// Another way you can use if you only care about 1 record is the find method
// this method will return the first item that satisfies the provided testing function
// otherwise it will be null
const record_filtered = record_view.records.find(record => record.name === "Test 1") the record {...}

if(record_filtered) {
  let test1 = "gui123"
  let recordId = record_filtered
  // you can use console.log too
  console.log("recordId:", recordId);

  await table.updateRecordAsync(recordId, {
      "Name" : test1,
  })
}

 

 The console.log will look like this:

jsep_0-1712791422690.png

I hope this helps. If you need more help feel free to schedule 15min call with me.