Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在index.js文件中不使用触发器函数从firestore数据库读取数据?_Javascript_Android_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 如何在index.js文件中不使用触发器函数从firestore数据库读取数据?

Javascript 如何在index.js文件中不使用触发器函数从firestore数据库读取数据?,javascript,android,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Android,Firebase,Google Cloud Firestore,Google Cloud Functions,我在firestore中存储了一些数据。有一个集合(图书)链接到文档(图书id),图书id有名称、图像、位置、标题等字段。 我有另一个集合(用户),它的文档(用户id)用户id字段作为令牌id。每当图书集合中有任何写入操作时,我都必须使用令牌id向所有用户发送通知。 如果我在firestore的index.js文件中有硬编码的令牌id,我可以向所有用户发送通知。 但我必须动态发送通知。我无法从集合用户读取用户id 'use-strict' const functions = require('

我在firestore中存储了一些数据。有一个集合(图书)链接到文档(图书id),图书id有名称、图像、位置、标题等字段。 我有另一个集合(用户),它的文档(用户id)用户id字段作为令牌id。每当图书集合中有任何写入操作时,我都必须使用令牌id向所有用户发送通知。 如果我在firestore的index.js文件中有硬编码的令牌id,我可以向所有用户发送通知。 但我必须动态发送通知。我无法从集合用户读取用户id

'use-strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

    exports.sendNotification = functions.firestore.document("Books/{book_id}").onWrite((change, context) => {
const book_id = context.params.book_id;

console.log("Book ID: " + book_id);

return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult => {

      const tokenid= queryResult.data();

     const token_id='fOGd94em4ik:APA91bHyZBGBYvO_ZFlLO1lWL1LU-r-1JkuF3fuKieWvV4HuPDKAiG5hdn-BQrMPFeICBdKZ3UR2nkM2PMxClEzVI3V2C38OxoP-1w71Dz-GbO0sbDlg-nswCMZ';

     const payload = {
            notification : {
                title : 'Hi',
                body : 'New Books list is available in the database! Please check the book store',
                icon : "default"
            }
        };

        return admin.messaging().sendToDevice(token_id, payload).then(result => {
            //console.log("Notification sent");
            return 0;
        });

});
});

在上面的代码中,我想从集合用户中读取用户id。我如何阅读它?因为它没有与藏书链接,我无法阅读。

如果我理解得很好,并且如果token_id是用户记录中的一个键,您可以尝试以下方法:

admin.firestore().collection('Users')
.where('token_id', '==', 'YOUR_TOKEN').get()
.then(snap => snap.docs.map(user => { 
   // iterate on your notification process
})

我想从集合用户获取所有令牌ID。我的数据库结构是Books->book id->field(名称、图像、标题)。另一个集合是Users->userid->field(令牌id)。如果我将从用户那里获得userid,那么我将能够获得令牌id…请给我获取userid的方法。我想这样的方法应该可以达到目的,尽管我必须承认我不明白为什么需要这样做。:
const uids=[];admin.firestore().collection('Users').get().then(snap=>snap.docs.map(user=>uids.push(user.id))