根据属性检查现有对象的firebase,防止重复 [英] Check firebase for an existing object based on attributes, prevent duplicates

查看:140
本文介绍了根据属性检查现有对象的firebase,防止重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的firebase,看看我是否已经存储了一个匹配两个属性的对象。把这个问题放到上下文中,设想一个只存储歌曲的应用程序,像artist和songname等一些属性,如下所示:

  var wishlist = new Firebase('https://myurl.firebaseio.com/wishes'); 
$ b $ wishlist.push({
'artist':artist,
'song':song,
'likes':0
});

如果已经存在具有相同艺术家和歌曲名的对象,我如何检查?文档似乎没有提供这样的例子,因为他们只指出直接引用名称的情况。我的对象名是自动生成的,我在回调中用 snapshot.name(); 来读取它们。

或者我应该使用firebase后端的安全规则来做到这一点,或在客户端检查它?



非常感谢任何线索:)!

解决方案

为每个条目存储索引,如下所示:

<$ p $ / wishes / $ wishid /(歌曲,艺术家)
/ wish_index / $ hash / $ wishid

使用 $ hash >散列算法

  var fb = new Firebase(URL); 
$ b $ function saveWishEntry(song,artist){
var wishRef = fb.child('wishes')。push();
wishRef.set({
歌曲:歌曲,艺术家:艺术家,喜欢:0
},函数(err){
if(err)throw err;
saveWishIndex(wishRef.name(),song,artist);
})
}

function saveWishIndex(key,song,artist){
// http: //phpjs.org/functions/md5/
var hash = MD5(歌曲)+':'+ MD5(artist);
fb.child('wish_index /'+ hash).set(key);

$ / code>

现在,当您想知道是否存在记录时,只需查看索引:
$ b $ pre $ //回调将传递一个{字符串}键或null(未找到)
函数wishId(song,artist,callback){
fb.child('wish_index /'+ hash).once('value',function(snap){
callback(snap.val());
});
}


I want to check my firebase to see if I already have an object stored that matches two attributes. To put this question into context, imagine an app that just stores songs, with attributes like 'artist' and 'songname' and some more, like this:

var wishlist = new Firebase('https://myurl.firebaseio.com/wishes');

wishlist.push({
   'artist': artist,
   'song': song,
   'likes': 0
});

I want to prevent object creation at this point if an object with the same artist AND songname already exists. How do I check for that? The documentation doesn't seem to provide an example for that since they only point out cases that directly reference a name. My object names are auto generated, I read them in the callback with snapshot.name();.

Can or should I do this using the security rules in the firebase backend or check for it on the client side already?

Thanks a lot for any clues :)!

解决方案

Store an index for each entry with something like the following:

/wishes/$wishid/ (song, artist)
/wish_index/$hash/$wishid

Create $hash using a hash algorithm:

var fb = new Firebase(URL);

function saveWishEntry( song, artist ) {
  var wishRef = fb.child('wishes').push();
  wishRef.set({
     song: song, artist: artist, likes: 0
  }, function(err) {
     if( err ) throw err;
     saveWishIndex(wishRef.name(), song, artist);
  })
}

function saveWishIndex(key, song, artist) {
  // http://phpjs.org/functions/md5/
  var hash = MD5(song)+':'+MD5(artist);
  fb.child('wish_index/'+hash).set(key);
}

Now when you want to know if a record exists, simply look it up in the index:

// callback will be passed either a {string}key or null(not found)
function wishId(song, artist, callback) {
   fb.child('wish_index/'+hash).once('value', function(snap) {
       callback( snap.val() );
   });
}

这篇关于根据属性检查现有对象的firebase,防止重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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