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)?
Related
I have, in the same database, the following two collections: users and posts.
Whenever a document inside posts is created server-side, an ObjectID field named author is added to the document that matches the author's _id.
For example:
As can be seen, the ObjectID properties are the same.
However, when I call the following aggregate method on the posts collection, the $lookup operation apparently fails, not adding an object.
Where id is an argument passed down to the function that contains the string for the post document, and postsCollection is require('../db.js').db().collection('posts');
What I expected
posts is an array containing just one object, that in turn contains the data for the post document with the _id property matching the incoming id argument.
This object should have all the properties from the document and one additional property named 'authorDocument' that contains all the data from the user document.
What happens
posts is an array containing just one object with all the data from the relevant document. However, the authorDocument property is not added.
This is what I get by calling console.log(posts[0]):
What I have tried
I've created another post and also relogged (to create another session). Same problem.
I've also tried finding the document with another method: collection.findOne and it worked with the author property from posts[0] object.
gets me
I have also tried comparing the author field with the _id field from the user, by doing this:
It returns true.
Turns out it was a very simple typo. I figured it while reading the documentation.
There it says:
I hadn't listed the operations inside an array of operations, and that's why the second operation ($lookup) was not being recognized.
By adding the following brackets, the code now runs perfectly:
I have an object req.user.stripe that has a value of an empty object {}.
I would like to use the lodash function isEmpty() to check if this object is empty or not. When I run console.log(_.isEmpty(req.user.stripe)), I get a return value of false. console.log(req.user.stripe) in fact returns an empty object {}.
Not sure what I am missing. I am open to using a different method aside from lodash. Essentially, I need to access properties within req.user.stripe, but only if req.user.stripe is not an empty object (otherwise I'll get an error saying that JS cannot read property x on undefined).
Clarification
In my mongoose schema, when a user is created, they are created with a stripe parameter, but it defaults to an empty object {}. Only when we actually talk to stripe does that fill up with data. I have a number of beta users in my system that were given accounts, but not through the same process as someone registering on the frontend. These users are now required to pay, but they have to keep their accounts. I need to detect if a user has stripe information or not, hence why I am checking if the stripe parameter in the req.user object is or is not an empty object.
For example, if I want to access that last 4 digits of a user's saved card, I can access it this way req.user.stripe.customer.sources.data[0].last4. However, when trying to access a user without stripe data, an error similar to Cannot read property 'sources' of undefined. I need to be able to handle conditions like this when the req.user.stripe object is empty.
Before accessing that property use an if statement:
if(req.user && req.user.stripe) { do_your_thing() }
There's three options here:
Your object isn't actually empty, it just appears empty on the console for some reason.
It's actually empty when you do the console.log, but not when you do _.isEmpty (As suggested by #Todd Chaffee in the comments; but I find this doubtful)
There's a bug in _.isEmpty. (I also find this one doubtful, but it's a possibility)
But, as others have said in the comments, I don't think _.isEmpty is what you want in the first place.
To take your example of req.user.stripe.customer.sources.data[0].last4: what if req.user.stripe isn't empty, but customer isn't defined, or it's empty, or it's defined but the sources.data array is undefined or empty, or etc? Sure, maybe all of those situations are completely impossible in the current code, but will they always be? _.isEmpty(req.user.stripe) is a fairly fragile test.
A safer alternative is _.get(req, "user.stripe.customer.sources.data[0].last4"), which will just return null if any of those intermediary properties are null.
...but frankly it smells of a larger structure issue that you're trying to get the last4 digits of a credit card without knowing whether or not you've even got a customer in the first place.
This is an old question, but using _.isNil(object) solved this problem for me, returns if object is null or empty.
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);
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
I'm getting a result (the object) back from a mongoose query and want to replace/remove some keys, before I stringify the object and send it to the client.
When I console.log the object, everything is nice (all the keys are listed, nothing more). When I do a for..in on the object, hidden fields popup out of nowhere (and the keys of interest, don't). They make sense - they belong to mongoose - but I don't want them. Same thing happens, when using Object.keys or Object.getOwnPropertyNames on the Object - hidden fields popup, none useful.
So I wanted to inspect the element with util.inspect(obj,true,null,true) and log the result to console. The result on the console is the same, as if I'd console.logged the object directly without the inspection.
Now, two questions;
How do I correctly inspect an object, so that I actually get internal/hidden information about the object?
How can I iterate over the object and only get the fields? (yes, I am doing the hasOwnProperty check within the for..in loop)
//EDIT
OK, I got it. After some investigation, I realized, that the mongoose object proxies its properties and has a toJSON function, which explains why the console.logs were in the expected output structure. The solution is to use Mongoose own toObject method;
mongooseObj.toObject({ getters: true, virtuals: false })
This way, I can iterate over Object.getOwnPropertyNames and replace/remove keys I don't want the client to know about!
I'm going to guess that you're looking at the Document object returned by a mongoose query when you really just want to see the data. If my guess is correct, you'll probably want something like this:
Model.findOne({ name: 'john' }, function (err, doc) {
var data = doc.toObject();
// do whatever you need to with data
delete data.age;
delete data.weight;
data.isAwesome = true;
// now stringify the data and do something with it
callback(JSON.stringify(data));
});
Also if you want to access a certain field from a Document you can take a look at:
Document#get
// path
doc.get('age') // 47
// dynamic casting to a string
doc.get('age', String) // "47"