【问题标题】:Listing records with Airtable API使用 Airtable API 列出记录
【发布时间】:2018-06-27 20:07:29
【问题描述】:

我有一个 Airtable 基础,我可以从中检索记录(请参见下面的代码),但我想获取除“位置”之外的其他字段的值。使用“console.log('Retrieved: ', record.get('Location'));”,除了“位置”字段之外,如何修改此行以在输出中包含名为“大小”的字段的字段值?我试过“console.log('Retrieved: ', record.get('Location', 'Size'));”,但是没有用。

这是我的代码的摘录:

// Lists 3 records in Bins 
base('Bins').select({
    // Selecting the first 3 records in Grid view:
    maxRecords: 3,
    view: "Grid view"
}).eachPage(function page(records, fetchNextPage) {
    // This function (`page`) will get called for each page of records.

    records.forEach(function(record) {
        console.log('Retrieved: ', record.get('Location'));
    });

    // To fetch the next page of records, call `fetchNextPage`.
    // If there are more records, `page` will get called again.
    // If there are no more records, `done` will get called.
    fetchNextPage();

}, function done(err) {
    if (err) { console.error(err); return; }
});

输出

检索到 170000118

检索到 170000119

检索到 170000120

【问题讨论】:

    标签: javascript airtable


    【解决方案1】:

    我发现这个repo 可以在我尝试制作这样的情况时提供帮助。 用于访问 airtable.com 数据库上的数据的常用函数的包装器。所有查询都返回承诺。

    如果您想避免使用 npm 包,以下是它的工作原理。但最终它的主要目的是使用请求或一些缺乏承诺履行的方法来检索记录。

    import Airtable from 'airtable'
    import _ from 'lodash'
    
    const ENDPOINT_URL = 'https://api.airtable.com'
    let API_KEY // Can only set the API key once per program
    
    export default class AirTable {
    
        constructor({apiKey, databaseRef}) {
            if(!API_KEY) {
                API_KEY = apiKey
                Airtable.configure({
                    endpointUrl: ENDPOINT_URL,
                    apiKey: API_KEY
                });
            }
            this.base = Airtable.base(databaseRef)
            this.get = {
                single: this.getSingleRecordFrom.bind(this),
                all: this.getAllRecordsFrom.bind(this),
                match: this.getAllMatchedRecordsFrom.bind(this),
                select: this.getRecordsSelect.bind(this)
            }
            this.insert = this.createRecord.bind(this)
            this.add = this.insert
            this.create = this.insert
    
            this.update = this.updateRecord.bind(this)
            this.set = this.update
    
            this.remove = this.deleteRecord.bind(this)
            this.delete = this.remove
            this.destroy = this.remove
            this.rem = this.remove
        }
    
        async createRecord({tableName, data}) {
            return new Promise((resolve, reject) => {
                this.base(tableName).create(data, (err, record) => {
                    if (err) {
                        console.error(err)
                        reject()
                        return
                    }
                    console.log("Created " + record.getId())
                    resolve(record)
                })
            })
        }
    
        async updateRecord({tableName, id, data}) {
            return new Promise((resolve, reject) => {
                this.base(tableName).update(id, data, (err, record) => {
                    if (err) {
                        console.error(err)
                        reject()
                        return
                    }
                    console.log("Updated " + record.getId())
                    resolve(record)
                })
            })
        }
    
        async deleteRecord({tableName, id, data}) {
            return new Promise((resolve, reject) => {
                this.base(tableName).destroy(id, (err, record) => {
                    if (err) {
                        console.error(err)
                        reject()
                        return
                    }
                    console.log("Deleted " + record.getId())
                    resolve(record)
                })
            })
        }
    
        async getSingleRecordFrom({tableName, id}) {
            console.log(tableName, id)
            return new Promise((resolve, reject) => {
                this.base(tableName).find(id, function(err, record) {
                if (err) {
                    console.error(err)
                    reject(err)
                }
                resolve(record)
                })
                    // console.log(record);
            })
        }
    
        async getAllRecordsFrom(tableName) {
            return this.getRecordsSelect({tableName, select: {} })
        }
    
        async getAllMatchedRecordsFrom({tableName, column, value}) {
            return this.getRecordsSelect({tableName, select: {filterByFormula:`${column} = ${value}`} }) // TODO: validate input
        }
    
        async getRecordsSelect({tableName, select}) {
            return new Promise((resolve, reject) => {
                let out = []
                this.base(tableName).select(select).eachPage((records, fetchNextPage) => {
                    // Flatten single entry arrays, need to remove this hacky shit.
                    _.map(records, r => {
                        _.forOwn(r.fields, (value, key) => { // If array is single
                            if(_.isArray(value) && value.length == 1 && key != 'rooms') {
                                r.fields[key] = value[0]
                            }
                        });
                    })
                    out = _.concat(out, records)
                    fetchNextPage();
                }, (err) => {
                    if (err) {
                        console.error(err)
                        reject(err)
                    } else {
                        // console.log(JSON.stringify(out, null, 4))
                        // console.log("HI")
                        resolve(out)
                    }
                })
            })
        }
    }
    

    希望这是有道理的,同时尝试让 API-Proxy 获取整个表,甚至使用 Express 来获取记录 id 作为数组也可以工作

    【讨论】:

    • 这应该同时访问所有表吗?!
    【解决方案2】:

    您可以使用此代码行。

    records.forEach(function(record) {
        console.log('Retrieved: ', record.get('Location') + ' ' + record.get('Size'));
    });
    

    【讨论】:

    • 不要只发布代码!既然您找到了一个好的解决方案,那么您就有了最好的知识来解释它
    • Airtable 支持该功能,因此您可以使用 record.get('field name') 获取 Airtable base 中字段的值。如果您还有其他问题,请随时问我。
    猜你喜欢
    • 2023-02-08
    • 1970-01-01
    • 2021-11-15
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多