1

我有一个 Airtable 基础,可以从中检索记录(请参见下面的代码),但我想获取除“位置”之外的其他字段的值。使用“ 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

4

2 回答 2

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,因为数组也可以工作

于 2018-09-26T05:29:15.667 回答
1

您可以使用此代码行。

records.forEach(function(record) {
    console.log('Retrieved: ', record.get('Location') + ' ' + record.get('Size'));
});
于 2019-12-19T20:41:59.510 回答