如何从NodeJS中的某个对象获取随机隔离值?我如何知道请求是否以及何时被满足?我的意思是没有这样做:从同一个巨型Firebase DB对象中检索几个随机快照
http.createServer(function(request, response) {
var obj = [];
ref("games").child(rnd(range)).once("value").then(function(snapshot) {
obj.push(snapshot.val());
}).then(function() {
ref("games").child(rnd(range)).once("value").then(function(snapshot) {
obj.push(snapshot.val());
}).then(function() {
ref("games").child(rnd(range)).once("value").then(function(snapshot) {
obj.push(snapshot.val());
}).then(function() {
ref("games").child(rnd(range)).once("value").then(function(snapshot) {
obj.push(snapshot.val());
}).then(function() {
ref("games").child(rnd(range)).once("value").then(function(snapshot) {
obj.push(snapshot.val());
}).then(function() {
ref("games").child(rnd(range)).once("value").then(function(snapshot) {
obj.push(snapshot.val());
}).then(function() {
response.end(JSON.stringify(obj));
});
});
});
});
});
});
}).listen(8081);
我似乎无法得到递归代码去,因为我是新来的这一点,有这么多的数据走动。
'RND(范围)'返回一些随机密钥,和你想得连续几个值? – qxz
@qxz它返回'Math.floor(Math.random()* range)'。没有什么花哨。 –