Apr 10, 2024 11:38 AM - edited Apr 10, 2024 12:59 PM
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,
})
Solved! Go to Solution.
Apr 10, 2024 04:25 PM
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:
I hope this helps. If you need more help feel free to schedule 15min call with me.
Apr 10, 2024 04:25 PM
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:
I hope this helps. If you need more help feel free to schedule 15min call with me.