使用 Airtable API 列出记录

Listing records with Airtable API

我有一个可以从中检索记录的 Airtable 库(请参阅下面的代码),但我想获取 "Location" 以外的其他字段的值。使用“console.log('Retrieved: ', record.get('Location'));”,我如何修改此行以在输出中包括 "Size" 字段以及 "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

我发现这个 repo 在我尝试处理此类情况时提供帮助。 用于访问 airtable.com 数据库上的数据的常用函数的包装器。所有查询 return 承诺。

如果你想避免使用 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 获取整个 table 甚至使用 Express 获取记录 ID,因为数组也可以工作

您可以使用此代码行。

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