Javascript 使用Airtable API列出记录

Javascript 使用Airtable API列出记录,javascript,airtable,Javascript,Airtable,我有一个Airtable库,可以从中检索记录(请参见下面的代码),但我想获取除“Location”之外的其他字段的值。使用“console.log('Retrieved:'、record.get('Location'));”,如何修改此行以在输出中除了“Location”字段外,还包括名为“Size”的字段值?我尝试了“console.log('Retrieved:',record.get('Location','Size'));”,但没有成功 以下是我的代码摘录: // Lists 3 rec

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

当我尝试处理类似的情况时,我发现这有助于解决问题。 用于访问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代理获取整个表,甚至使用Express获取记录id,因为数组也可以工作

您可以使用此代码行

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

不要只是张贴代码!既然您找到了一个好的解决方案,您就有了最好的知识来解释它。Airtable支持该功能,因此您可以使用record.get('field name')获取Airtable base中字段的值。如果您还有其他问题,请随时问我。这是否应该一次访问所有表?!