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

rlcwz9us  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(125)
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...”
测试_评分
学术:1
艺术:4人
体育:3
insta_handle:“ab”
twitter_handle:“hj”

b09cbbtk

b09cbbtk1#

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

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'){

字符串
你可以声明(在云函数之外)一个函数来检查一个变量是否为undefined或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) && ......) { 
          .....
        }
.......

相关问题