Firebase deep querying - javascript

This question is about implementing firebase deep querying. Consider the following structure in firebase:
Here, my ref is pointing to the root of the structure which is /messages. So I have :
var ref = new Firebase("https://cofounder.firebaseio.com/messages");
I wish to query those message Id's having member = -752163252 . So basically the returned object should be the one with key 655974744 . How do I go about doing this in firebase?
Here's what I tried in my console:
ref.orderByChild("members").equalTo(235642888).on('value', function(snap){console.log("Found ",snap.val())});
Result was:
VM837:2 Found null
I sense there is a missing link somewhere. Basically, I want to know if there is any way to query the deep nested data without having the parent key id's (in this case 25487894,655974744) .
Also, if I may extend my question, is there a way to add a listener that calls back when a new messageId (in this case 25487894,655974744) is added containing member = -752163252 .
Hope my question is clear enough. Any help is greatly appreciated!
EDIT:
I have already looked at the dinosaurs example, and that's how I tried what I tried but it didn't work.

Your query asserts that the "members" node is an integer with value 235642888, which it is not, naturally. It's an object containing a list of keys.
Instead, you would want to use the deep child query syntax and something like the following:
ref.orderByChild("members/235642888").equalTo(235642888);
Note that you don't really need the value of the key to be the key itself. You could save storage by just setting this to a binary true or 1. The query would be much the same:
ref.orderByChild("members/235642888").equalTo(true);

Related

What does .$ mean in javascript

I was in the console trying something when i saw that the .$ can be used along with the document object to access the elements. But i don't know what it actually does.
example :-
After some detective work, my guess it is a special Polymer component property:
Automatic node finding
Polymer automatically builds a map of statically created instance nodes in its local DOM, to provide
convenient access to frequently used nodes without the need to query
for them manually. Any node specified in the element's template with
an id is stored on the this.$ hash by id.
I am not familiar with Polymer, and it is very difficult to find (recent) documentation on this property.
However I believe my guess is correct based on the description above and the screen shot below. As you can see, if you add another . after the $ you get a list of suggested properties. These are all ids in the DOM:
I guess that may be any global object under jquery. Exactly, I don't have any experience with .$ but surf the official docs of Jquery, it may help or another possibility is that it is something coming from backend of from database. There may be a lot of reason. Hope, it helps..
This should be a property added to the element but is not anything special.
An example would be like
let a = {};
a.$ = {
b: 1,
c: 2
};
console.log(a.$);
This will also give you the object properties of $ in a.
A bonus fun fact is, in Javascript, emoji is also valid as property name, and therefore
let a = {};
a.$ = {
"😍": 1,
"😎": 2
};
console.log(a.$["😍"]);
also works

Access Array with String key

I have two variables with JSON files. The first is a list of keys looks like this:
keylist = ["key1","key2","key3"]
The second one is generated from a database and looks like this:
data = {
"key1"{
#further data
},
"key2"{
#further data
},
"key3"{
#further data
}
}
Now I want to access the second element of the database with the key from the keylist
data.keylist[1];
Which doesn't work because the return of keylist[1] is a String? I did some research and the use of the window function was proposed. So I tried this:
window["data." + keylist[1]]();
Which leads to a "is not a function" error. What can I do to solve this problem?
As simple as that:
const mydata = data[ keylist[1] ];
Also, your code is correct from the point of syntax, but it tells completely different than you expect it to tell.
data.keylist[1];
tells JS that you expect to have an object called data which has a property called keylist and which is (most likely) type of array, and you want to get the second element of this array.
PS: And one more point here. Your question title is not completely correct because of the difference between Arrays and Object in JS.
There is no "string keys" for arrays in JS, so you cannot "access array with a string key". Well, truly speaking there are, but not for items of array. Array items only have numeric index, which you can use to access it. Objects, in contrast to arrays, may have named properties. So when you see something like that: data = myVar['data'], you can tell that you're dealing with an object, while data = someVar[0] can be both, an Array (most likely) or also an Object with key named '0'.
I don't think the issue you're having with your first example is because it returns a key. I believe the issue is because data doesn't have a property called keylist. Instead of that, try it as
data[keylist[1]]
and see if that works for you. The reason this one should work is that, in this situation, Javascript will evaluate the string return of keylist[1] and then use it as a string index for the data variable. Let me know if this works out for you :D
You can try using using something like this.
data[keylist[1]]

How do I access the 'str' value in my object?

I am trying to return the value under the key 'str' in an Object but I am having trouble accessing the value.
This is what is returned in the console:
Currently I am using a map function to go over the array and just return the _str value like so:
let idx = currentArray.map(function(x) {
return x._id._str;
});
However it is still returning the value as an object. How can I get just the value of the _str key?
Here is the full array without specifying the id field. This is what is returned if you jsut return 'x' in the map function.
You've clarified that the screenshot is of x._id. So to access _str, you'd use x._id[0]._str: The _str property is in the object referenced by the 0 property (the first entry in the array x._id refers to).
Note that in general, _-prefixed properties are meant not to be accessed by code outside the code responsible for the objects in question. You don't seem to be responsible for them, so accessing those properties is likely to make your code rely on undocumented properties that may change in the next "dot" release of whatever lib you're using. It's just convention, but it's a very common convention.
If you right click on the property, most browser consoles offer the ability to copy property path.
Based on this SO post and the docs, it appears that you can probably use x._id.str.
If I understand correctly, you are receiving the str value but it is an object instead of the string literal. In other words, you are getting _str: "598..." instead of "598....". A possible solution would be to use the mongo javascript function to convert the str value to a string.
In your case, I think something like return x._id.str; may work as _id is a MongoID.ObjectID.
I've also linked the documentation below for reference.
https://docs.mongodb.com/manual/reference/method/ObjectId/
Here's a relevant SO answer as well: Convert ObjectID (Mongodb) to String in JavaScript
I think you should write x[_id]._str because _id is one of the array objects.

Can't check if data exists in firebase due to unique id container

I am using .push() to store data in firebase. Due to this it is contained inside a unique id, so for example:
ref.push({
myObject: {
title: "Some Title"
}
});
This is how it appears inside a database:
I need to check if data that I am pushing to the firebase does not already exist in there, so compare myObject that is being pushed to all myObject instances stored in the database. I can't figure how to do this as there are these unique id's that nest myObject instances inside them.
Note: this is just an example in reality myObject contains way more children than title, therefore each of these children need to be compared, hence whole myObject.
Reference to firebase: var ref = new Firebase('https://myurl.firebaseio.com/Objects');
I tried figuring this out with security checks, in app code, but this unique id really confuses me in how it nests myObject.
Related resource: http://firebase.com
As you want to compare complete objects (all properties), the correct solution is to generate a hash string on save. This hash would practically be you "unique id / primary key", so you'll easily know if exists and optionally overwrite/merge/etc...
in firebase, don't use push (as you don't want the priority), but keep set your structure to: /myObjects/<hashkey> = {...} or with another supporting index list, as suggested by #mccannf link here: Check firebase for an existing object based on attributes, prevent duplicates

Include pointers in array of pointers in Parse Javascript query

I know how to load the objects pointed in a pointer array in Parse (thanks to this link: https://www.parse.com/questions/include-pointer-in-array-of-pointers). However, I need to load objects pointed inside the array of pointers, too (which would be something equivalent to triple JOINs in SQL). I am querying a class that has an array of pointers, this works:
query.include("myArrayColumn.pointer");
It loads all the Post objects that are pointed inside the array. However, these Post object also have a _User pointer, and I need to get that _User object, too. I've tried:
query.include("myArrayColumn.pointer.user");
Unfortunatelly, user objects aren't fetched. Their id's are there, but trying to access anything other than id (e.g. username) returns undefined.
Is there a way to eager-load the users, or do I have to use a second query (or some other mechanism)?

Categories