代码之家  ›  专栏  ›  技术社区  ›  ajeet sharma

如何在云函数中获取firebase数据库数据?

  •  1
  • ajeet sharma  · 技术社区  · 7 年前

    我正在使用firebase数据库存储我的iOS应用程序数据。

    enter image description here

    我正在将用户跟踪数据保存在此应用程序中,该应用程序运行良好。

    我必须使用我保存在“IOS”字段中的推送令牌向用户(userID=57411405)发送推送通知。

    我正在使用的云函数:

    此云功能运行良好。我能够跟踪保存新跟踪数据的事件。以下是此云功能的日志:

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    
    // Create and Deploy Your First Cloud Functions
    // https://firebase.google.com/docs/functions/write-firebase-functions
    
    exports.databasechanges = functions.database.ref('/users/{id}/LocationTracking').onWrite(event =>{
    
    var eventSnapshot = event.data
    
    console.log('UserId - ', event.params.id);
    
    const userID = event.params.id
    const root = event.data.ref.root
    
    admin.database().ref('users/{id}/NotificationToken').on('value').then((snapshot) => {
    
      var token = snapshot.val().IOS;
    
      console.log('token',token)
    
      return snapshot.val();
    }).catch(error => {
      console.error(error);
      res.error(500);
    });
    
    return eventSnapshot.val()
    
    });
    

    但在云功能控制台上,我遇到了以下错误:

    enter image description here 现在,我不知道如何访问这个推送令牌(IOS)并使用云功能发送推送通知。

    1 回复  |  直到 5 年前
        1
  •  4
  •   Peter Haddad    7 年前

    要获取字段“IOS”,请尝试以下操作:

    //inside the trigger function
    admin.database().ref('/users/'+event.params.id+'/NotificationToken/IOS').once('v‌​alue').then((snapshot) => { 
    var token=snapshot.val().IOS;
    
     });