【发布时间】:2016-12-28 08:59:08
【问题描述】:
如何从 NodeJS 中的对象中获取随机隔离值?我如何知道请求是否以及何时得到满足?我的意思是不这样做:
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(range)返回一些随机键,而您尝试连续获取多个值? -
@qxz 返回
Math.floor(Math.random() * range)。没什么特别的。
标签: node.js firebase firebase-realtime-database database nosql