Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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/2/node.js/35.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 在NodeJS中的单个函数中传递回调两次_Javascript_Node.js_Callback_Firebase - Fatal编程技术网

Javascript 在NodeJS中的单个函数中传递回调两次

Javascript 在NodeJS中的单个函数中传递回调两次,javascript,node.js,callback,firebase,Javascript,Node.js,Callback,Firebase,我创建了一个从firebase获取值的函数。现在,存储firebase查询结果的变量只能在firebase操作内部访问。但是我需要函数外部的变量,所以我创建了回调函数来解决这个问题 我的代码如下所示: 我有两个firebase数据库。 一个是存储注册用户(参考1),另一个是存储付费用户(paidRef)。我需要检查谁有登录是一个注册用户或付费用户 var paidRef=new Firebase("https://app.firebaseio.com/paidUsers"); var ref1=

我创建了一个从firebase获取值的函数。现在,存储firebase查询结果的变量只能在firebase操作内部访问。但是我需要函数外部的变量,所以我创建了回调函数来解决这个问题

我的代码如下所示: 我有两个firebase数据库。 一个是存储注册用户(参考1),另一个是存储付费用户(paidRef)。我需要检查谁有登录是一个注册用户或付费用户

var paidRef=new Firebase("https://app.firebaseio.com/paidUsers");
var ref1=new Firebase("https://app.firebaseio.com/tempUser");

function checkPaidUsers(res,callback){

 ref1.orderByChild('userId').equalTo(jsonData.userId).once('child_added', function(snap) {

 registeredUser=true;
  paidRef.on('child_added',function(snapshot) {
   if(snapshot.child('userId').val()==jsonData.userId )
   {

      paidFlag=true;
      return callback(registeredUser,paidFlag,res);
   }
   else
   {

      paidFlag=false;
      return callback(registeredUser,paidFlag,res);
   }

 })

})
}
checkPaidUsers( res,function(registeredUser,paidFlag) {

  if(registeredUser!=true)
  {
     newUser=true;


  }
return res.send({paidFlag:paidFlag,registeredUser:registeredUser,newUser:newUser});})
此代码给出如下错误:

Can't set headers after they are sent.
之所以出现此错误,是因为调用回调函数的次数与paidRef的子级数相同,因为如果在paidRef数据库中找不到用户,它将转到else块并执行回调函数。
从一个回调函数中获取注册用户和付费用户的所有信息的最佳解决方法是什么。

您的问题是您要为paidRef中的每个用户调用一次回调,这似乎不是您的意图。
这段代码应该只调用回调一次

var paidRef=new Firebase("https://app.firebaseio.com/paidUsers");
var ref1=new Firebase("https://app.firebaseio.com/tempUser");

function checkPaidUsers(res,callback){

 ref1.orderByChild('userId').equalTo(jsonData.userId).once('child_added', function(snap) {

 registeredUser=true;
  paidRef.child(jsonData.userId).once('value', function(snapshot) {
    var paidFlag = false;
    if (snapshot.val() !== null) {
      paidFlag = true;
    }
    callback(registeredUser, paidFlag, res)
  })
})
}
checkPaidUsers( res,function(registeredUser,paidFlag) {

  if(registeredUser!=true)
  {
     newUser=true;


  }
return res.send({paidFlag:paidFlag,registeredUser:registeredUser,newUser:newUser});})

您的问题是您正在为paidRef中的每个用户调用一次回调,这似乎不是您的意图。
这段代码应该只调用回调一次

var paidRef=new Firebase("https://app.firebaseio.com/paidUsers");
var ref1=new Firebase("https://app.firebaseio.com/tempUser");

function checkPaidUsers(res,callback){

 ref1.orderByChild('userId').equalTo(jsonData.userId).once('child_added', function(snap) {

 registeredUser=true;
  paidRef.child(jsonData.userId).once('value', function(snapshot) {
    var paidFlag = false;
    if (snapshot.val() !== null) {
      paidFlag = true;
    }
    callback(registeredUser, paidFlag, res)
  })
})
}
checkPaidUsers( res,function(registeredUser,paidFlag) {

  if(registeredUser!=true)
  {
     newUser=true;


  }
return res.send({paidFlag:paidFlag,registeredUser:registeredUser,newUser:newUser});})

如果使用
ref1.orderByChild('userId').equalTo(jsonData.userId)
进行查询,为什么不将用户存储在其userId下:
/tempUsers/$uid
。然后,您可以很容易地找到您要查找的用户
ref1.child(jsonData.userId)。一旦('value',函数(snapshot){console.log(snapshot.val();})
如果您使用
ref1.orderByChild('userId').equalTo(jsonData.userId)
进行查询,为什么不将用户存储在其userId:
/tempUsers/$uid
下。然后,您就可以很容易地找到要查找的用户
ref1.child(jsonData.userId).once('value',function(snapshot){console.log(snapshot.val();})