触发器函数内的所有变量获取值后,实时数据库触发器

时间:2018-05-14 12:35:56

标签: node.js firebase firebase-realtime-database google-cloud-functions

const functions = require('firebase-functions');
var admin = require('firebase-admin');
var request = require('request');
var firebase = require('firebase');

admin.initializeApp();
firebase.initializeApp();

exports.takeValues = functions.database.ref('/users/{uid}/')
.onWrite((change, context) => {

       var user_id = context.auth.uid;
       var FB_access_token = change.after.val().Facebook_Access_Token;
       var twitter_username = change.after.val().twitter_handle;
       var insta_username = change.after.val().insta_handle;
       var test_at_src = change.after.val().Test_Score.Arts;
       var test_sp_src = change.after.val().Test_Score.Sports;
       var test_ac_src = change.after.val().Test_Score.Academics;

       if( FB_access_token!==undefined||'undefined' && twitter_username!==undefined||'undefined' && insta_username!==undefined||'undefined' && user_id!==undefined||'undefined' && test_at_src!==undefined||'undefined' && test_ac_src!==undefined||'undefined' && test_sp_src!==undefined||'undefined'){ 
        apicall(user_id,FB_access_token,twitter_username,insta_username,test_ac_src,test_at_src,test_sp_src)
      }
    });

    function apicall(user_id,FB_access_token,twitter_username,insta_username,test_ac_src,test_at_src,test_sp_src){

        return new Promise((resolve, reject)=> {

           var dataString = `user_id=${user_id}&FB_access_token=${FB_access_token}&twitter_username=${twitter_username}&insta_username=${insta_username}&test_ac_src=${test_ac_src}&test_at_src=${test_at_src}&test_sp_src=${test_sp_src}`;             

            var options = {
                url: 'http://ip/api',
                method: 'POST',
                body : dataString

            };

            function callback(error, response, body) 
{
    if (!error && response.statusCode === 200)
     {
         console.log(body);
         resolve(body);
    }
}
request(options, callback);
});
      }

每次在firebase数据库上写入数据时,上面的代码都会调用apicall()函数,即使所有变量都没有值。但是,只有在所有变量都具有值之后,才需要调用api的代码,即apicall()函数。可以做些什么?

数据库如下所示:

  

TtUBAOgi0aWBdPou2oCu

     
    

Facebook_Access_Token:" .... kwBAHXDtyxSauOZAnzENAnA3d88Igbxxjzz2 ..."

         

Test_Score

         
      
        

学者:1

                 

艺术:4

                 

体育:3

      
    
         

insta_handle:" ab"

         

twitter_handle:" hj"

  

1 个答案:

答案 0 :(得分:1)

原因是您检查数据的方式未定义不正确,即以下代码不正确:

if( FB_access_token!==undefined||'undefined' && twitter_username!==undefined||'undefined' && insta_username!==undefined||'undefined' && user_id!==undefined||'undefined' && test_at_src!==undefined||'undefined' && test_ac_src!==undefined||'undefined' && test_sp_src!==undefined||'undefined'){ 

您可以声明(在Cloud Function之外)一个函数,该函数检查变量是否未定义或为null,并按如下方式使用它:

admin.initializeApp();
firebase.initializeApp();

function testIsDefined(v) {    // <- new function, returns true if not undefined and not null
   return (typeof(v) != 'undefined' && v != null)
}

exports.takeValues = functions.database.ref('/users/{uid}/')
.onWrite((change, context) => {

       var user_id = context.auth.uid;
       var FB_access_token = change.after.val().Facebook_Access_Token;
       var twitter_username = change.after.val().twitter_handle;
       var insta_username = change.after.val().insta_handle;
       var test_at_src = change.after.val().Test_Score.Arts;
       var test_sp_src = change.after.val().Test_Score.Sports;
       var test_ac_src = change.after.val().Test_Score.Academics;

       if( testIsDefined(FB_access_token) && testIsDefined(twitter_username) && testIsDefined(insta_username) && ......) { 
          .....
        }
.......