I want to count the total data in a users profile in my firebase. Therefore I first get the profile as object:
var profile = $firebase(ref.child('profile').child(userId)).$asObject();
Now If I console.log this var there is this object:
I found some solutions to count the object with
console.log(Object.keys(profile));
But this gives me only the count 3. always:
Anyone got a solution how I can get the count of the data stored?
There should be age, gender, place, realname, username = 5
Try with
profile.$loaded.then(function(data) {
console.log(data);
});
Take a look at the first comment in this
post
Related
Please I have a history page on my flutter app that I want to work on and I have been thinking of a way to store the data correctly in my firestore so that I can achieve the desired result as seen in the picture below
I would love to have data of thesame day stored in thesame container as seen in the picture above. My challenge is, I do not know how to structure my data to get the desired result.
here is what I have tried;
my Count class file is below although I am not sure if that's what I will really do.
class Count {
String id;
final int count;
final createdOn;
Count({this.id = '', required this.count, required this.createdOn});
Map<String, dynamic> toJson() =>
{'id': id, "count": count, "createdOn": createdOn};
Count.fromSnapShot(DocumentSnapshot<Map<String, dynamic>> snapshot)
: id = snapshot.id,
count = snapshot.data()!["count"],
createdOn = snapshot.data()!["createdOn"];
}
and this is where I send data to firestore using onpressed in button
onPressed: () async {
exerciseCounter.increment();
final counter = exerciseCounter.count;
final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = await auth.currentUser;
final uid = user?.uid;
final percents = FirebaseFirestore.instance
.collection('exercise-percentage')
.doc(uid)
.collection("daily-percentage");
final percent = Count(
count: counter,
createdOn: FieldValue.serverTimestamp());
final json = percent.toJson();
await percents.add(json);
},
now I am not sure of the correct way to use streambuilder to get the data from firestore and join the data of thesame day in thesame container. I understand that I will need to use query method to query my data with the firestore serverTimeStamp but I don't know how to use it to fetch data of thesame day and display it as shown in the picture above.
I will appreciate it if someone can really help me out. It can just be with a simple example I can follow or by correcting and adding to my code. Thank you for your time.
I would love to have data of the same day stored in the same container
as seen in the picture above.
You could have an extra field beside the createdOn one which holds the date in the following format YYYYMMDD.
This way, you can easily:
Query the Firestore document for a specific day
Use a set of count aggregation queries to calculate the KPIs/Counters for a specific day
I'm pretty new, so bear with me.
I'm making a Google script that will let me call the TMDb API and get some information from a movie list I'm compiling for myself. I'm trying to get all the values to automatically fill by just using the TMDB ID.
After struggling, I found it was easiest to create a function for each column I want to fill (title, date, genre, poster url, etc.) and pass the input from the cell in the spreadsheet to be able to retrieve the info, then return the data to that cell.
I can't figure it out though, when it comes to the "genre" category, because it's in an array.
Here's my code that works for a different column:
function getPoster(input) {
var movieID = input
// Call the TMDB API for movie details
var response = UrlFetchApp.fetch("https://api.themoviedb.org/3/movie/" + movieID + "?api_key=<<mykey>>");
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
return data["poster_path"];
}
Using the API data:
"poster_path":"/hXLScIERphkTsMGdfKKvF4p4SPB.jpg",
However, the "genre" category lists them as an array:
"genres":[{"id":28,"name":"Action"},{"id":18,"name":"Drama"},{"id":53,"name":"Thriller"}],
How can I write the return so that it sends a string with the shown genres "Action, Drama, Thriller" into the single corresponding cell (not spilling into adjacent cells), while also ignoring the "id"?
you need to create an array from the genre's name values.
use this snippet:
var genres = [{"id":28,"name":"Action"},{"id":18,"name":"Drama"},{"id":53,"name":"Thriller"}];
// Return "Action, Drama, Thriller" as a string into a single cell
return genres.map(function(genre){
return genre.name;
}).join(", ");
Sample output when printed:
I am trying to create a new record for a model and there are fields I need to save later after the model is created.
I have the code like this
let new_model = await Model.create({ name, type}).fetch();
new_model.content = 'abc';
new_model.save();
TypeError: new_model.save is not a function
I googled, it doesn't seem like sailsjs can be done this way so I guess I have to use the update or updateOne by sails. But the thing is, name,type fields are not unique and will not be so using update will actually turn into update for ALL records. updateOne will give me errors saying there's more than one record though
Does anyone has any suggestions on what can be done here?
One other option I can think of is doing something like this with updateOne
let new_model = await Model.create({ name, type}).fetch();
// getting your content here or do something else
const content = 'abc';
new_model = await Model.updateOne({ id:new_model.id}).set({content});
I need to get the name of each like on each post on a group page. Since I first have to get some data from the original post, I'm trying to make a connection then iterate through the likes and get a list of the users who liked each post. Here's what I have:
Connection<Likes> feedLikes = postFeed.fetchConnection(id+"/likes", Likes.class, Parameter.with("fields","from,actions,type"));
// Get the iterator
Iterator<List<Likes>> likeIt = feedLikes.iterator();
while(likeIt.hasNext()) {
List<Likes> likeFeed = likeIt.next();
for (Likes currLike: likeFeed) {
String ObjectId = id;
String LikeUserId = currLike.getId();
String LikeUserName = currLike.getName();
like_data.add(new String[] {ObjectId, LikeUserId, LikeUserName});
}
}
This doesn't work and I'm a little stuck on why. I know the username is stored in Likes.LikeItem but I can't even get to that step so far. Does anyone have any idea what I'm missing?
According to the Facebook reference this is not possible (https://developers.facebook.com/docs/graph-api/reference/v3.2/object/likes):
A User or Page can only query their own likes. Other Users'or Pages' likes are unavailable due to privacy concerns.
Only aggregated counts using total_count with the summary parameter are available for Post likes.
Say I'd like to fetch only items that contains keys: "-Ju2-oZ8sJIES8_shkTv", "-Ju2-zGVMuX9tMGfySko", and "-Ju202XUwybotkDPloeo".
var items = new Firebase("https://hello-cambodia.firebaseio.com/items");
items.orderByKey().equalTo("-Ju2-gVQbXNgxMlojo-T").once('value', function(snap1){
items.orderByKey().equalTo("-Ju2-zGVMuX9tMGfySko").once('value', function(snap2){
items.orderByKey().equalTo("-Ju202XUwybotkDPloeo").once('value', function(snap3){
console.log(snap1.val());
console.log(snap2.val());
console.log(snap3.val());
})
})
});
I don't feel that this is the right way to fetch the items, especially, when I have 1000 keys over to fetch from.
If possible, I really hope for something where I can give a set of array
like
var itemKeys = ["-Ju2-gVQbXNgxMlojo-T","-Ju2-zGVMuX9tMGfySko", "-Ju202XUwybotkDPloeo"];
var items = new Firebase("https://hello-cambodia.firebaseio.com/items");
items.orderByKey().equalTo(itemKeys).once('value', function(snap){
console.log(snap.val());
});
Any suggestions would be appreciated.
Thanks
Doing this:
items.orderByKey().equalTo("-Ju2-gVQbXNgxMlojo-T")
Gives exactly the same result as:
items.child("-Ju2-gVQbXNgxMlojo-T")
But the latter is not only more readable, it will also prevent the need for scanning indexes.
But what you have to answer is why want to select these three items? Is it because they all have the same status? Because they fell into a specific date range? Because the user selected them in a list? As soon as you can identify the reason for selecting these three items, you can look to convert the selection into a query. E.g.
var recentItems = ref.orderByChild("createdTimestamp")
.startAt(Date.now() - 24*60*60*1000)
.endAt(Date.now());
recentItems.on('child_added'...
This query would give you the items of the past day, if you had a field with the timestamp.
You can use Firebase child. For example,
var currFirebaseRoom = new Firebase(yourFirebaseURL)
var userRef = currFirebaseRoom.child('users');
Now you can access this child with
userRef.on('value', function(userSnapshot) {
//your code
}
You generally should not be access things using the Firebase keys. Create a child called data and put all your values there and then you can access them through that child reference.