首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Airtable API列出记录

用Airtable API列出记录
EN

Stack Overflow用户
提问于 2018-06-27 20:07:29
回答 2查看 2.5K关注 0票数 1

我有一个Airtable基地,我可以从其中检索记录(请参阅下面的代码),但我希望获得除"Location“之外的其他字段的值。使用"console.log('Retrieved: ', record.get('Location'));",除了“位置”字段之外,我如何修改这一行,以便在输出中包含一个名为"Size“的字段的字段值?我试过"console.log('Retrieved: ', record.get('Location', 'Size'));",但那行不通。

,以下是我代码的摘录:

代码语言:javascript
复制
// 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-26 05:29:15

当我试图生产这样的情况时,我发现这个存储库可以帮助我。用于访问airtable.com数据库上数据的通用函数的包装器。所有查询都会返回承诺。

如果您想避免使用npm包,下面是它的工作原理。但最重要的是,要么使用请求,要么使用一些缺乏承诺的方法来检索记录。

代码语言:javascript
复制
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的as数组也可以工作。

票数 1
EN

Stack Overflow用户

发布于 2019-12-19 20:41:59

您可以使用此代码行。

代码语言:javascript
复制
records.forEach(function(record) {
    console.log('Retrieved: ', record.get('Location') + ' ' + record.get('Size'));
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51070635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档