Adding document reference to field - reactjs

I wish to add a reference to /user/ID inside one of the fields (called user, and is of type "reference") of my charity collection . As for timestamp you can use firestore.FieldValue.serverTimestamp(), is there something equivalent to this?
I did try by adding it as a string, but that doesn't work as expected.
--
In other words, how do I populate this, by code (specificly React Native)?

Appreantly, it is sufficent to do:
const userReference = firestore().collection('users').doc(ID);
and use that reference on push.

Related

FireStore and maps/arrays, document-list to array in Kotlin

I've finally started to understand a lot of info regarding FireStore, but I'm wondering if I can get some assistance.
If I had a setup similar to or like this:
          races
                Android
                      name: Android
                      size: medium
                       stats          <---- this is the map
                                str: 10
                                sex: 12.... (more values)
How would I parse this? I am looking to make specific TextViews apply values found in the database so that I can simply update the database and my app will populate those values so that hard coding and code updating won't be nearly as troublesome in the future.
I currently use something like this:
val androidRef = db.collection("races").document("Android")
androidRef.get().addOnSuccessListener { document ->
if (document != null) {
oneOfTheTextViews.text = document.getString("str")
} else {
}
The issue is currently I can only seem to access from collection (races) / document (android) / then a single field (I have "str" set as a single field, not part of a map or array)
What would the best practice be to do this? Should I not nest them at all? And if I can reference said nesting/mapping/array, what functions need to be called? (To be clear, I am not asking only whether or not it is possible - the reference guides and documents allude to such - but what property/class/method/etc needs to be called in order to access only one of those values or point to one of those values?).
Second question: Is there a way to get a list of document names? If I have several races, and simply want to make a spinner or recycler view based on document names as part of a collection, can I read that to the app?
What would the best practice be to do this?
If you want to get the value of your str property which is nested within your stats map, please change the following line of code:
oneOfTheTextViews.text = document.getString("str")
to
oneOfTheTextViews.text = document.getString("stats.str")
If your str property is a number and not a String, then instead of the above line of code please use this one:
oneOfTheTextViews.text = document.getLong("stats.str")
Should I not nest them at all?
No, you can nest as many properties as you want within a Map.
Is there a way to get a list of document names?
Yes, simply iterate the collection and get the document ids using getId() function.

Change a parameter type of a Lodash function

I'm newbie to Lodash and I use it within TypeScript coding. So I have an array of objects called items (of foods), every item has a category object parameter (with id & name attributes), so it looks like this :
I want to group those items by categroy names, I've created a new Object of type any called foodLists (public foodLists: any;),
and after loading those items, I've tried this:
this.foodLists = lodash.keyBy(this.items, 'category.name');
Actually, it did a part of the work, so it appears like this:
So the items are sorted by category , the issue is that it afftected just an only item to each category , however I want that many items should be affected to each category
So the second parameter of lodash.keyBy() (which is here 'category.name') should accept an array of objects. Hope you understand me. Is that possible ? or is there any alternative in lodash that gives the wanted result ? and Thank you.
it seems the solution is _.groupBy
lodash.groupBy(this.items, 'category.name')

Query objects where the pointer object matches any objects within a PFObject array

My class Posts has a column that is a pointer to another class called Styles. Each post must be associated to a Style object as a rule of thumb.
My problem: I can't get only the posts that are associated to one or more styles.
My object selectedStyles, that is an array of PFObjects that already contains the style objects I would like to use to match the query. So populating the selectedStyles is not an issue, but how to use it to produce my query is.
What I am doing at the moment is:
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Posts")
query.whereKey("bellongsToStyle", containsAllObjectsInArray: [selectedStyles])
query.orderByDescending("createdAt")
return query
If I could translate the whereKey method in plain english I would say:
...contains - ANY OF THE - PFObjectsInArray [selectedStyles]
I am not so sure if that is possible... any ideas?!
I think I am too late for this but you can just add
[IncludeKey:"bellongsToStyle"]
when you are querying in your 'Posts' class
don't need to run any extra query for that
here's a small reference
http://blog.parse.com/announcements/queries-for-relational-data/

What is the difference between a model object queried by filter and an object queried by get() in Django?

I keep coming across this issue where I am trying to update a record using the update() method.
It always works when I query an object using filter.
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.filter(pk=1)
thing[0].update(**my_dictionary) wrote it wrong in the original question.
thing.update(**my_dictionary)
When I query the object using get() it keeps telling me that the object has no method update()
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.get(pk=1)
thing.update(**my_dictionary)
Isn't a model object the same in both cases? Why would one have an update method and the other one not? Any insight would be greatly appreciated.
The documentation is very explicit about this:
filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
If you know there is only one object that matches your query, you can use the get() method on a Manager which returns the object directly.
Your first snippet returns a QuerySet, which has an update method. The second snippet returns a model instance, which doesn't.
Note that you have not shown the exact code you are using: thing[0].update would give exactly the same error as the second snippet.
You're using QuerySet.update() and ModelInstance.save().
If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory. For example, instead of doing this:
e = Entry.objects.get(id=10)
e.comments_on = False
e.save()
...do this:
Entry.objects.filter(id=10).update(comments_on=False)

Inserting an add button after a field_reference

I would like to modify the form class so for every field_reference it adds a button + so users are able to directly insert a new element into that referenced table.
i.e: I'm completing a customer form and cannot found his city in the list. I just click an the + and the city form appears and let me insert the correct city.
I think I have to try by modifying atk4/lib/Controller/MVCForm.php around
if($field instanceof Field_Reference || $field_type=='reference') {
$form_field->setModel($field->getModel());
}
But maybe it's something to add in atk4/lib/field/Reference.php directly.
Try out this one:
https://github.com/atk4/autocomplete/blob/master/lib/Form/Field/Plus.php
It's not exactly what you're asking for, but this field type has implemented "plus" behavior. You can at least get some ideas from there if not using autocomplete field

Resources