【发布时间】:2018-05-14 21:51:44
【问题描述】:
更多解释:
- 在 iOS 上创建了一个文档。它包含一个城市和一个类型。
- 在 CloudFunctions 中,我通过
onWrite触发器检测到文档创建/更新。 - 我想在其他 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