当前位置: 首页 > 知识库问答 >
问题:

在Firestore云函数中创建文档时,我希望进行查询以获得兼容的文档,然后向它们发送通知

祁建明
2023-03-14

更多解释:

    null
exports.NewRequestCreated = functions.firestore.document('/Requests/{documentId}').onWrite(event => {
// Get an object with the current document value

var myRequest = event.data.data();

// Get an object with the previous document value (for update or delete)
// Note that previous will be null if no old value exists.
if (event.data.previous) {
  var oldDocument = event.data.previous.data();
}

//Get list of donors that can match this request
var compatibleTypesArray = getCompatibeUsers(myRequest.Type);
var matchingUsersArray = [];


var usersRef = functions.firestore.document('/Users');//functions.firestore.document('/Users');
var matchingUsersQuery = usersRef.where('city', '==', myRequest.city).where('type', '==', myRequest.type)
var user = matchingUsersQuery.get()
.then(snapshot => {
    snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
    });
})
.catch(err => {
    console.log('Error getting documents', err);
});

TypeError:usersRef.where在导出时不是函数。EXPORTS.NewRequestCreated在Object上创建了created.functions.firestore.document.onwrite.event(/user_code/index.js:45:40)。(/user_code/node_modules/firebase-functions/lib/cloud-functions/lib/cloud-functions.js:59:27)在next(本机)在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71在__awaiter(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)在

共有1个答案

周高畅
2023-03-14

usersref是一个文档。正如您将在API文档中看到的,该类上没有where()方法。

也许您打算使用collection()获取一个CollectionReference对象,然后可以使用where()方法查询文档。

functions.firestore.collection('/Users');
 类似资料:
  • 我试图实现的很简单。只有当数据库不存在时,我才需要在数据库中创建一个用户条目。 应用程序流程: 使用Firebase身份验证(获取UID)创建用户 客户端代码(创建操作): firestore规则: 附加信息: 正如您可能已经知道的那样,代码不起作用。每次我运行客户端代码时,当前用户都会被一个新文档完全替换。但是,如果我从规则中删除以下行,一切都会正常工作: 但现在问题来了,如何保护用户文档中的数

  • 在我们仔细处理之后。我仍然无法获取自动生成的文档ID。没有“then”选项。我的代码: 创建后如何获取ID?

  • 正如我从“云火数据模型”指南中得到的,“每个文档都由一个名称标识。”是否可以通过该文档标识符(即名称或ID)查询集合? 例如,集合“Things”中的文档具有IDs、1、2等。: 是否可以查询 ID 小于 100 的文档?

  • 当用户购买产品并按条支付时,我在自动化工作流程的集成方面有点困难。我正在使用火力还原和云功能。 工作流程 > 用户通过条纹Checkout.js购买产品 付款存储在付款集合中 触发云功能(付款收款的onWrite) TODO:将文档添加到集合“用户”文档中的“采购”集合 费用支付 除了第4步和第5步之外,我已经实现了这个工作流,因为我不知道如何在云函数中从Firestore检索和添加DocRef(

  • 我想在firestore数据库中查询文档id。目前我有以下代码: 我没有结果。但当我查询其他字段时,它会起作用: 文档id的名称是如何调用的?

  • 我有一个云Firestore DB,其结构如下: null null 有人能想出一个优雅的解决这个问题的办法吗?