Firebase Reference not returned after add - firebase

I do something like this:
var temp = $scope.names.$add({"firstname" : $scope.firstname, "lastname" : $scope.lastname, "school" : $scope.selectedSchool});
I get a returned object, but when I try
$log.log(temp.toString());
I get just the object, whereas the documentation for firebase says I should see the path to the string of my new firebase URL. I also tried
$log.log(temp.name());
And same result, is this related to the implementation of those "functions" in angularfire, or to actual firebase itself?
I'm trying to syncronize adding "names" to a names portion of my json object, and then following it up with creating a section in my json object where I can get more details of the individual and so I want to match up the key's
kinda like this JSON Data structure resulting in the least passed data from database
I don't see how to get the above to work, and I saw (at one point, can't find the link now), that firebase was recommending using transaction not the "once" operation, so I was hoping I would get the individuals key, then pass that into creating a new firebase "subobject?" that matched it.

Related

How do i access a double nested response from firebase realtime database?

database structure
so this is my database structure and i'm trying to access the key "amount"
i have tried accessing it like this:
code for accessing and assigning
but i keep getting this error ==
error code
please i'm stuck
The orderData from which you are trying to obtain the information is another Map<String, dynamic>, so you first need to access the value of that one, before you can call orderData['amount'], orderData['dateTime'], etc...
You can do this with
orderData = orderData.values.elementAt(0);
This assumes that orderData always contains at least 1 element, so it might be good to check for this and in case it is not true, throw/handle an Exception.

VueFire: Resolving references after setting on the client

According to the docu "[...] VueFire will automatically bind up to one nested references.". That works well, if I retrieve an object (map) from the database with a property being a ref: The ref gets ressolved automatically on the client (ref_property will not hold the path to the object (e.g. users/123) but the actual data ({username: 'john', hometown: 'autumn'}).
Question is: How do I update a ref_property (e.g. suppose a last_edit_by_ref) on the client in a way, that a.) VueFire is able to resolve this to a valid JSON for the UI and b.) make sure that it's stored as a ref in the database at the same time?
I tried to fetch the referenced object (again) from the collection as explained here ("To write a reference to a document, you pass the actual reference object"). The issue with this is however, that VueFire does not ressolve this, leading to empty values in the UI:
post.last_edit_by_ref = db.collection('users').doc('123')
Background: If I'm setting plain JSON, the property is no longer stored as a reference in the database. This is bad, since the linked object is likely to be changed (and the linking objekt would then hold copied, outdated data).
It does not related to VueFire. It is how firebase parse the Object it get in the set/update methods.
If you focus on this part:
const data = {
age: 18,
name: "John",
carRef: db.collection('cars').doc('john-car'),
}
await db.collection('users').doc('john').set(data);
You will have the ref in firestore. And in turn, VueFire will automatic bind the object.
For your case, i think you will need to find a way to get the db.collection('users').doc(last_edit_user_id) to make the ref for post.

How to remove Cloud Firestore field type specifiers when using REST API?

I totally made up the name "type specifiers." What I mean is the stringValue key in front of a value. Usually I would expect a more-standard response: "name" : "name_here".
{
"fields": {
"name": {
"stringValue": "name_here"
}
}
}
Is it possible to remove those when making a GET call?
More importantly, it be nice to understand why it's structured like it is. Even for POST-ing data? The easy answer is probably because Cloud Firestore, unlike Realtime Database, needs to know the specific types, but what are all the deeper reasons? Is there an "official" name for formatting like this where I could do more research?
For example, is the reasoning any related to Protocol Buffers? Is there a way to request a protobuf instead of JSON?
Schema:
Is it possible to remove those when making a GET call?
In short No. The Firestore REST API GET returns an instance of Document.
See https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents#Document
{
"name": string,
"fields": {
string: {
object(Value)
},
...
},
"createTime": string,
"updateTime": string,
}
Regarding the "Protocol Buffer": When the data is deserialized you could just have a function to convert into the structure you wish to use, e.g. probably using the protocol buffers if you wish but as there appear to be libraries for SWIFT, OBJECTIVE-C, ANDROID, JAVA, PYTHON, NODE.JS, GO maybe you won’t need to use the REST API and craft a Protocol Buffer.
Hopefully address your “More Importantly” comment:
As you eluded to in your question Firestore has a different data model to the Realtime Database.
Realtime database data model allows JSON objects with the schema and keywords as you want to define it.
As you point out, the Firestore data model uses predefined schemas, in that respect some of the keywords and structure cannot be changed.
The Cloud Firestore Data Model is described here: https://firebase.google.com/docs/firestore/data-model
Effectively the data model is / where a document can contain a subcollection and the keywords “name”, “fields”, “createdTime”, “upTime” are in a Firestore document (a pre-defined JSON document schema).
A successful the Firestore REST API GET request results in a Document instance which could contain collection of documents or a single document. See https://firebase.google.com/docs/firestore/reference/rest/. Also the API discovery document helps give some detail about the api:
https://firestore.googleapis.com/$discovery/rest?version=v1beta1
An example REST API URL structure is of the form:
https://firestore.googleapis.com/v1beta1/projects/<yourprojectid>/databases/(default)/documents/<collectionName>/<documentID>
It is possible to mask certain fields in a document but still the Firestore Document schema will persist. See the three examples GET:
collection https://pastebin.com/98qByY7n
document https://pastebin.com/QLwZFGgF
document with mask https://pastebin.com/KA1cGX3k
Looking at another example, the REST API to run Queries
https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/runQuery
the response body is of the form:
{
"transaction": string,
"document": {
object(Document)
},
"readTime": string,
"skippedResults": number,
}
In summary:
The Realtime database REST API will return the JSON for the object according to the path/nodes as per your “more-standard response”.
The Firestore REST API returns a specific Firestore predefined response structure.
There API libraries available for several language so maybe it’s not necessary to use the REST API and craft your own Protocol Buffer but if you needed to you it’s probably feasible.
I don't understand why somebody just say that you can't and don't try think some solution for help! Seriously that this is a really problem solver?
Anyway, I created a script that will help you (maybe it's late now hahaha).
The script encode json and after replace it as string to modify and remove Google type fields (low process).
It's a simple code, I know that you can improve it if necessary!
WARNING!!
Maybe you will have problems with values that contain '{}' or '[]'. This can be solved with a foreach that convert all strings that contains this elements in other char (like '◘' or '♦', some char that you know that doesn't will be in value.
Ex.: Hi {Lorena}! ------> Hi ◘Lorena♦!
After the process, convert again to '{}' or '[]'
YOU CAN'T HAVE FIELDS WITH THE SAME NAME THAT GOOGLE FIELDS
Ex.: stringValue, arrayValue, etc
You can see and download the script in this link:
https://github.com/campostech/scripts-helpers/blob/master/CLOUD%20STORE%20JSON%20FIELDS%20REMOVER/csjfr.php

Why is meteor adding ObjectId around the _id field since today?

Maybe I'm completely wrong, but since projects that I start today, when I use meteor mongo to connect directly to the mongodb, and insert a record (with mycol.insert(..) the _id field is surrounded with ObjectId("12345555...").
When adding a record from code this is not the case.
So, records added via Meteor Mongo are not recognised inside the app any longer.
I have done this in the past so often...
what's happening here?
That's the MONGO way and you see this because you are using the Mongo shell. Meteor defaults to a different method (cf below) which you see when you use it programmatically. Check Meteor docs on new Mongo.Collection
idGeneration String
The method of generating the _id fields of new documents in this collection. Possible values:
'STRING': random strings
'MONGO': random Mongo.ObjectID values
The default id generation technique is 'STRING'
In Meteor, if you write
Steffo = new Meteor.Collection("steffo", {idGeneration: 'STRING'});
this will result in entries
{ "foo" : "bar", "_id" : "68FWFNGRAuRt82pWy" }
If you use
Paul = new Meteor.Collection("paul", {idGeneration: 'MONGO'});
you'll get
{ "foo" : "bar", "_id" : ObjectId("26cfdb5f200adfa0b55a50d3" }
The latter happens when you use Mongo shell.

strange wcf rest error

I will start off by saying I am new to wcf and rest. Here is the situation I have created a wcf service, the service returns an object(person) that contains fields and a collection of order objects. When I create an instance, the object and the collection of orders gets populated as expected
When I call the same service via rest call returning json see below, the code fails. if i remove the code where the object is populated, it works. the json shows and empty set of orders.
<OperationContract()> _
<WebGet(UriTemplate:="GetPerson/{user}", ResponseFormat:=WebMessageFormat.Json)> _
Function GetPerson(ByVal User As String) As Budget
The line that fails looks like this
lobjPerson.OrderList.Add(order)
Order list has been instantiated, and works, but not when called via rest call returning json
The error i get in the browser is "the error is kcferrorDomainWinsock:10054". If i look at with web inspector I can't seem to see an error. Hopefully this rings a bell for someone.
I fixed my problem from switching from arraylist to list(of t) for my collections. I am still pondering why this worked, can anyone explain this?. Second question remaining is how do you debug rest calls to wcf? anyone?

Resources