Google App maker record key value - google-app-maker

In App maker we enabled the manual save mode. On button click a new form will open and we will create an empty record, when user fills the fields and clicks the save button saveChanges function will save all the values.
In documentation and sample projects I can see after a record creation _key value is updated in the data source and we can use that key value to query record from its child model.
But in our case key value is not returned. But after save changes function when we open that record key value is coming, what could be the issue.

You don't have record key on the client, until you save it, since record key is generated by the server. This applies both to Auto and Manual save modes.
Here is code snippet from App Maker documentation:
var myCreateDatasource = app.datasources.MyDatasource.modes.create;
var draft = myCreateDatasource.item;
draft.Name = "Name";
draft.Age = 21;
// Create the new item
myCreateDatasource.createItem(function(newRecord) {
// Callback is called after the record is saved, so it now has a key.
var key = newRecord._key;
// Do something with the key here.
}

Related

Flutter: I want to get data from firebase realtime database, everytime the application loads

I managed to get the data from the firebase real-time database, but I can't use it inside the app. Based on the data, I want to show different screens (Activities). For example: If the user paid then show the "Paid" screen, otherwise show the "Pay Now" screen. I couldn't use the variable used inside eg: paid_status , outside from the below scope. So, I declared it as a global variable in another file.
package_stream.listen((DatabaseEvent event) {
globals.paid_status = event.snapshot.value;
});
With the above code, it works but every time, the user closes the app and login again, the variable value is changed or kept old (after the database is updated). I want to get the value from the database every time the user opens the app or logout and login again.
I used below code to get the data:
setState(() {
DatabaseReference paid_stat = db_reference.child('Records/${uid}/Paid');
// Get the Stream
Stream<DatabaseEvent> package_stream = paid_stat.onValue;
// Subscribe to the stream!
package_stream.listen((DatabaseEvent event) {
globals.paid_status = event.snapshot.value;
});
});
I solved it myself :
I created an async Future function using get() to return the snapshot.
Using the snapshot.value, I showed different screens/activities.

How to clone a record and create a new records in app maker?

I have a table. I want to clone a record. I added a button and in the OnClick event I have tried
var rowDataSource = widget.datasource;
// Instead of using the row datasource for create, explicitly use the data source of your list.
var listDatasource = app.datasources.Change;
var createDataSource = listDatasource.modes.create;
createDataSource.item.Systems.SystemName = rowDataSource.item.Systems.SystemName;
createDataSource.item.changeType = rowDataSource.item.changeType;
widget.datasource.createItem();
widget.datasource.saveChanges();
But this gave me error
can't associate Data with Draft Records since I am using relational Database and my databases are in manual save mode.
How to clone all field from one record and create a new record??
Here Author name comes from the relational database and Timestamp is from the tables database.

Google App Maker how to save and update records?

We have methods to create and update the item in App Maker, but it will save the whole record. If I want to update only a particular field on the button click, there is no option.
Please post if there are any options to update a particular field?
Automatic Save Mode
In auto save mode App Maker instantly saves every field, so you have per-field saving granularity. In other words, whenever field's value is changed App Maker sends request to server to save the modification.
// this code will trigger async call to server to save the modification
// only for this particular field.
app.datasources.MyDatasource.item.MyField = 'My new value';
Manual Save Mode
With manual save mode there is no easy way to save modifications subsets separately, since whenever you call 'saveChanges' method App Maker will try to persist all modifications made to the datasource. Here are some pretty bad workarounds that I would not recommend unless you have no other options:
// Implementation with callback chaining if field save
// order matters. It will work extremely slow.
var ds = app.datasources.MyDatasource;
ds.item.MyField1 = 'My new value 1';
ds.saveChanges(function() {
ds.item.MyField2 = 'My new value 2';
ds.saveChanges(function() {
ds.item.MyField3 = 'My new value 3';
...
});
});
// Implementation without chaining. In theory should work
// faster(if it would work at all...)
var ds = app.datasources.MyDatasource;
ds.item.MyField1 = 'My new value 1';
ds.saveChanges();
ds.item.MyField2 = 'My new value 2';
ds.saveChanges();
ds.item.MyField3 = 'My new value 3';
ds.saveChanges();
...
Server Script
Pretty much same answer as for Manual Save mode, it is doable, but I would not recommend do it since performance will significantly degrade.
record.MyField1 = 'My new value 1';
app.saveRecords([record]);
record.MyField2 = 'My new value 2';
app.saveRecords([record]);
record.MyField3 = 'My new value 3';
app.saveRecords([record]);
...

Update user's collection sub-object with index auto increment

In a method, I need to write in the user's collection a new token on each call.
I need also to stock every token (no replacement or udpate of a single token).
I'm using sub-objects (like multi user's emails) : how can I auto increment dynamically the index, to write each new token 'after' the last written?
For example, if there is already 6 tokens written (so Auth.0.token to Auth.5.token), the new one will be automatically Auth.6.token.
Thanks
Below my working static code.
var authtoken = Random.id([20]);
Meteor.users.update({_id: Meteor.userId()}, {$set: {"Auth.1.token": authtoken}});
Just $push the token onto the end of the array instead of trying to set then value.
Meteor.users.update({_id: Meteor.userId()}, {$push: {Auth.token: authtoken}});

Firebase,iOS: Appending key-value pair into a child node

I have the following User Table structure in Firebase
As you can see in the user that I have opened, I have a Posts section, inside this post section holds the Id's all articles which have been posted by this user.
The issue I am facing is as follows:
When the user creates a new article it's saved within the Posts Table, after the save I return the newly generated ID which I then pass on to the user table, I trying to insert the newly created ID into the post section of the user, so I assumed the URL would be something like this:
Users/{UserId}/Posts
However all this does it create a new section called posts, it doesn't actually insert the record into the given area.
My code which isn't working is as follows:
let linkPost = [childautoID: true]
FIRDatabase.database().reference().child("Users/\(UserId)/Posts").child(UserId).setValue(linkPost)
FYI the two id's that are currently inside Posts I added manually.
I've also tried the following:
FIRDatabase.database().reference().child("Users/\(UserId)/Posts").setValue(linkPost)
However all this does it remove all existing Id's and then inserts the new id.
I prefer something like this. This automatically append the data without fetching first
FIRDatabase.database().reference().child("Users/\(UserId)/Posts").child(UserId).setValue(true)
To append a key-value pair in Firebase Database child node use this :-
Make a Firebase Database Reference to the Posts node of that currentUser FIRDatabase.database().reference().child("Users").child(FIRAuth.auth()!.currentUser!.uid).child("Posts")
Check if Posts node exists in your user's DB, If not then create one by :- parentRef.setValue([postId : "True"]) in else block.
But if Posts node does exist retrieve it as a NSMutableDictionary , set the new object to it, and then store the updated Dictionary to that node.
func storePostsToDB(postID :String!, userID : String! = FIRAuth.auth()!.currentUser!.uid){
let parentRef = FIRDatabase.database().reference().child("Users").child(userID).child("Posts")
parentRef.observeSingleEventOfType(.Value, withBlock: {(friendsList) in
if friendsList.exists(){
if let listDict = friendsList.value as? NSMutableDictionary{
listDict.setObject("True", forKey: postID)
parentRef.setValue(listDict)
}
}else{
parentRef.setValue([postID : "True"])
}
})
}
Calling the function:-
storePostsToDB("yourPostID")// If you want to store in the currentUser DB
storePostsToDB("yourPostID", userID : otherUserID)//If you want to store the post in some other users database with uid `otherUserID`

Resources