如何避免Firebase中的类似行为的行为

时间:2018-01-08 05:49:00

标签: javascript firebase google-cloud-functions

Firebase以这种方式运作:

// we send this
['a', 'b', 'c', 'd', 'e']
// Firebase stores this
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}

// since the keys are numeric and sequential,
// if we query the data, we get this
['a', 'b', 'c', 'd', 'e']

// however, if we then delete a, b, and d,
// they are no longer mostly sequential, so
// we do not get back an array
{2: 'c', 4: 'e'}

点击此处查看更多相关信息:http://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

你知道如何避免这种奇怪的行为吗? 我想保存{0:91, 1: 99, 2: 998}并获得我保存的内容。我不想要我想要[91, 99, 998]的数组{0:91, 1: 99, 2: 998}

这是我用来保存的代码:

// This is goig to add a new key in the object
app.post('/push/another/value', (req, res) => {
  admin.database()
    .ref('/games/').child(req.body.gameId)
    .child('/solved/').child(req.body.i).set(req.body.newValue);
});

这是获得它的代码:

app.post('/get/all/values', (req, res) => {
  admin.database().ref('/games/').child(req.body.gameId)
    .once('value').then(value => {
      let game = value.val();

      game.solved = game.solved || {};

      res.send({
        status: 'ok',
        data: {
          game: game
        }
      });
    });
});

非常感谢你!

0 个答案:

没有答案