【问题标题】:Upon document creation in Firestore cloud functions, I want to make a query to get compatible documents, then send them notifications在 Firestore 云功能中创建文档后,我想查询以获取兼容的文档,然后向它们发送通知
【发布时间】:2018-05-14 21:51:44
【问题描述】:

更多解释:

  1. 在 iOS 上创建了一个文档。它包含一个城市和一个类型。
  2. 在 CloudFunctions 中,我通过 onWrite 触发器检测到文档创建/更新。
  3. 我想在其他 FireStore 文档中查询符合 City + 类型条件的用户。然后我想向他们发送通知。

这是我的代码:

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);
});

});

但这在 Functions Console 中失败并出现以下错误:

TypeError: usersRef.where 不是函数 在出口。 exports.NewRequestCreated Created.functions.firestore.document.onWrite.event (/user_code/index.js:45:40) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) 在下一个(本机) 在 /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) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) 在 /var/tmp/worker/worker.js:695:26 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

【问题讨论】:

  • 除了 Doug 的回答,请注意执行异步处理的 Cloud Functions(例如 get())必须返回 Promise。这在the documentation 中有解释。因为您将添加代码来发送 FCM 消息,这也是一个异步操作,所以您还需要熟悉链式 Promises。链接文档中的视频很有帮助。

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


【解决方案1】:

usersRefDocumentReference。正如您将在 API 文档中看到的那样,该类没有 where() 方法。

也许您的意思是使用collection() 获取CollectionReference 对象,然后您可以使用where() 方法查询文档。

functions.firestore.collection('/Users');

【讨论】:

    猜你喜欢
    • 2021-05-07
    • 2018-05-19
    • 2020-04-28
    • 2019-09-09
    • 2018-10-22
    • 1970-01-01
    • 2019-12-03
    • 2018-10-04
    • 2018-09-03
    相关资源
    最近更新 更多