Firebase JS数组丢失范围

时间:2018-02-18 09:54:49

标签: javascript firebase firebase-realtime-database

Firebase数据如下

-users
--demo1
---conid:1
-election
--election1
---conRegex:1
--election2
---conRegex:1

检索election1的代码,选举2是

var conid;
    var conRegex;
    var electionArr = [];
    if(uidAnonymous != null) {
        db.ref('users/'+user).once('value', function(snapshot) {
            if(snapshot.exists()) {
                conid = snapshot.val().conid;
            }
        });
        db.ref('election/').once('value', function(snapshot) {
            snapshot.forEach(function(electionSnapshot) {
                conRegex = electionSnapshot.val().conRegex;
                if(conid.startsWith(conRegex)) {
                    electionArr.push(electionSnapshot.key.toString());
                }
            });
        });
        console.log(electionArr);
    }

问题是electionArr是空的吗?我该如何解决这个问题

2 个答案:

答案 0 :(得分:2)

Firebase函数调用为on。多数是它如何工作你不能在firebase once(value)函数调用内的范围之外分配一个数组。在var conid; var conRegex; if(uidAnonymous != null) { db.ref('users/'+user).once('value', function(snapshot) { if(snapshot.exists()) { conid = snapshot.val().conid; } }); db.ref('election/').once('value', function(snapshot) { var electionArr = []; snapshot.forEach(function(electionSnapshot) { conRegex = electionSnapshot.val().conRegex; if(conid.startsWith(conRegex)) { Object.keys(electionSnapshot.val()).map(k => { electionArr.push(electionSnapshot.val()[k]); }) } }); console.log(electionArr); }); } 功能中使您的阵列可用。

q_prob = function(r, delta_t, sigma) {
  u = exp(sigma*sqrt(delta_t))
  d = exp(-sigma*sqrt(delta_t))

  return((exp(r*delta_t) - d)/(u-d))
}

build_stock_tree = function(S, sigma, delta_t, div_yield , T ,N) {
  tree = matrix(0, nrow=N+1, ncol=N+1)

  u = exp(sigma*sqrt(delta_t))
  d = exp(-sigma*sqrt(delta_t))

  for (i in 1:(N+1)) {
    for (j in 1:i) {
      tree[i,j] = S * u^(j-1) * d^((i-1)-(j-1)) * exp(-div_yield * delta_t *N)
    }
  }
  return(tree)
}

value_binomial_option = function(tree, sigma, delta_t, r, X, type) {
  q = q_prob(r, delta_t, sigma)
  option_tree = matrix(0, nrow=nrow(tree), ncol=ncol(tree))
  if(type == 'put') {
    option_tree[nrow(option_tree),] = pmax(X - tree[nrow(tree),], 0)
  } else {
    option_tree[nrow(option_tree),] = pmax(tree[nrow(tree),] - X, 0)
  }
  for (i in (nrow(tree)-1):1) {
    for(j in 1:i) {
      option_tree[i, j] = ((1-q)*option_tree[i+1,j] + q*option_tree[i+1,j+1])/exp(r*delta_t)#change here for American Option
    }
  }
  return(option_tree)
}

stock=

binomial_option = function(type, sigma, T, r, X, S, div_yield , N) {
  q = q_prob(r=r, delta_t=T/N, sigma=sigma)
  tree = build_stock_tree(S=S, sigma=sigma, delta_t=T/N, div_yield = div_yield, N=N)
  option = value_binomial_option(tree, sigma=sigma, delta_t=T/N, r=r, X=X, type=type)
  delta = (option[2,2]-option[2,1])/(tree[2,2]-tree[2,1])
  return(list(q=q, stock=tree, option=option, price=option[1,1], delta=delta))
}

delta = function(binomial_option, row, col) {
  stock_tree = binomial_option$stock
  option_tree = binomial_option$option
  return((option_tree[row+1, col+1] - option_tree[row+1, col])/(stock_tree[row+1, col+1] - stock_tree[row+1, col]))
}


binomial_option(type='call', sigma=0.2, T=3, r=0.1, X=100, S=150, div_yield = 0.05, N=3)

答案 1 :(得分:1)

once()函数is asynchronous。在读取所有数据时,您必须嵌套读取并将数组记录到控制台:

var conid;
var conRegex;
var electionArr = [];
if(uidAnonymous != null) {
    db.ref('users/'+user).once('value', function(snapshot) {
        if(snapshot.exists()) {
            conid = snapshot.val().conid;

            db.ref('election/').once('value', function(snapshot) {
                snapshot.forEach(function(electionSnapshot) {
                    conRegex = electionSnapshot.val().conRegex;
                    if(conid.startsWith(conRegex)) {
                        electionArr.push(electionSnapshot.key.toString());
                    }
                });
                console.log(electionArr);
            });
        }
    });
}