【问题标题】:Cannot encode type ([object Object]) to a Firestore Value error while attempting pagination尝试分页时无法将类型 ([object Object]) 编码为 Firestore 值错误
【发布时间】:2020-10-10 06:21:57
【问题描述】:

所以这里有我的代码

notifull.get('/getNote', (request, response) => {

// START Get variables
var requestData = {
    // location properties
    subject: request.query.subject,
    category: request.query.category,
    subcategory: request.query.subcategory,

    // easy way to get full reference string
    referenceString: function() {
        return `${this.subject}/${this.category}/${this.subcategory}`;
    },

    // pagination properties
    pagePosition: Number(request.query.pagePosition),

    // easy way to get limit number

    paginationNumber: function() {
        return (this.pagePosition - 1) * 2;
    }
};

// DEBUG_PURPOSES response.send(requestData.referenceString());

// END Get variables 


// START Construct index
var first = admin.firestore().collection(requestData.referenceString())
    .orderBy("upvotes")
    .limit(requestData.paginationNumber());

// DEBUG_PURPOSES response.send(first)

// END Construct index

// START Paginate
return first.get().then(function (documentSnapshots) {
    // Get the last visible document
    var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
    console.log("last", lastVisible);

    // Construct a new query starting at this document,
    // get the next 25 cities.
    var next = admin.firestore().collection(requestData.referenceString())
            .orderBy("upvotes")
            .startAfter(lastVisible)
            .limit(2);

    response.send(next)
});

});

如您所见,我正在尝试使用 Cloud Firestore 进行分页。如果您注意到,我已将代码分成多个部分,之前的测试显示构造索引和获取变量部分有效。但是,当我从 Firebase 自己的文档中提取 Paginate 示例,将其调整为我的代码,然后尝试运行时,我遇到了这个错误。

 Cannot encode type ([object Object]) to a Firestore Value

更新:经过更多测试,如果我删除 startAfter 行,它似乎可以正常工作。

【问题讨论】:

    标签: firebase pagination google-cloud-functions google-cloud-firestore


    【解决方案1】:

    根据 firebase 文档,自定义类的对象在 PHP、C++、Python 和 Node.js 等某些语言中不受支持。

    请在此作为参考。 https://firebase.google.com/docs/firestore/manage-data/add-data#custom_objects

    因此,我可以建议将您的对象编码为 json,并在您从 firebase 下载它们时将它们解码为您的自定义类。

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2016-11-08
      • 2017-04-10
      • 2018-07-25
      相关资源
      最近更新 更多