Firebase-更新条目,其中值是 [英] Firebase - Update entry where value is

查看:74
本文介绍了Firebase-更新条目,其中值是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取Firebase唯一ID,以便可以使用它来引用特定条目?

How do I get the Firebase unique id, so I can use it to reference a specific entry?

我正在使用push方法存储一些数据,每个条目都包含一个weekNumber和year.我检查一个WeekNumber是否已经存在,如果需要,我需要更新foo和bar.检查weekNumber是否存在是没有问题的,但是我不知道如何更新其他字段.

I am using the push method to store some data, each entry contains a weekNumber and year. I check if a weekNumber already exists, if so I need to update foo and bar. Checking if the weekNumber exists is no problem, but I can't figure out how to update the other fields.

示例数据结构:

- ENTRIES
 - USER
  -JwphpyzzHaNlh8HXA2G
   foo: 'string'
   bar: 'string'
   weekNumber: 32
   year: 2015

  -Jwphpyzzaasd2H2DHB
   foo: 'string'
   bar: 'string'
   weekNumber: 33
   year: 2015

检查是否存在weekNumber:

Checking if weekNumber exists:

ref.orderByChild('weekNumber').equalTo(data.weekNumber)
   .on('value', function(result) {

   });

我用result.key()或result.parent()尝试过,但是都返回了路径ENTRIES/USER而不是唯一的FB键.

I tried it with result.key() or result.parent(), but both return the path ENTRIES/USER and not the unique FB key.

推荐答案

使用当前数据结构,您可以通过以下方式获取数据:

With your current data structure, you can get the data with:

var query = ref.orderByChild('weekNumber').equalTo(data.weekNumber)
query.on('value', function(snapshot) {
    snapshot.forEach(function(weekSnapshot) {
        console.log(weekSnapshot.val());
        weekSnapshot.ref.update({ foo: 'newString' });
    });
});

但是我会考虑一个不同的数据结构.

But I would consider a different data structure.

Firebase的push()非常适合项目没有自然键的集合,类似于您将其放入JavaScript数组中的方式.

Firebase's push() is great for collections where the items don't have a natural key, similar to how you'd put them in an array in JavaScript.

但是,在您的情况下,这些项目具有自然键.看来您正在尝试按年+周的方式访问项目,(通过查看示例数据)似乎是一年中一周的唯一标识符.

But in your case, the items have a natural key. It looks like you're trying to access items by year+week, which (from looking at the sample data) seems like a pretty unique identifier for a week in a year.

在这种情况下,我会将数据存储在年+周"密钥下,如下所示:

In that case, I would store the data under a year+week key as:

- ENTRIES
 - USER
  "201532"
   foo: 'string'
   bar: 'string'
   weekNumber: 32
   year: 2015

  "201533"
   foo: 'string'
   bar: 'string'
   weekNumber: 33
   year: 2015

使用这样的结构,您可以直接访问特定的星期/年份:

With a structure like this, you can directly access a specific week/year:

ref.child('2015' + data.weekNumber).on('value', function(snapshot) {
    console.log(snapshot.val());
    snapshot.ref.update({ foo: 'newString' });
});

但是您仍然可以使用以下任意一种查询年份:

But you can also still query for year, with either of these:

// this will print all weeks of 2015
var query = ref.orderByKey().startAt('2015').endAt('201553');
query.on('child_added', function(snapshot) {
    console.log(snapshot.val());
});

或者甚至是每年的特定一周:

Or even by a specific week in each year:

// this will print week 33 of each available year
var query = ref.orderByChild('weekNumber').equalTo('33');
query.on('child_added', function(snapshot) {
    console.log(snapshot.val());
});

这篇关于Firebase-更新条目,其中值是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆