Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm developing an app in which each user must log-in first then they are able to add some objects . every objects they create will be save on firebase . but i want to separate user's obeject ! for example if user A save X and user B save Y , they must only access to their objects(user A must only have access to X) , but i don't know why anybody adds anything others have access to it ! got any idea?
in order to achieve your criteria , add an child attribute to your object node called user (the user who added that object) and then you could do a filter while requesting the data this way :
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("Object");
Query query = mDatabaseReference.orderByChild("user").equalTo("the_user_name").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
//here you get your object data
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
If you're trying to ensure that users can only access their own data, have a look at Firebase's server side security for Firestore, or for Realtime Database. These allow you to determine what data each user can access by specifying rules that are enforced on the server, so that they can't bypassed by any malicious client.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I have a project that's due in 3 days and this is the last step and I can't make it work. I connected MongoDB to NetBeans by downloading mongo-java-driver-2.13.3.
I have a homepage from which user will enter information in any of the fields (street, borough, cuisine, grade, name) and you must display the result back to user. If no restaurant matches the entered information, display a message to the user that no result. My code prints all info. I want it to be in text area and only print selected info.
Here is what I wrote:
private void btnfindActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:
DBCursor cursor =table.find();
while (cursor.hasNext())
{
//jTextArea1.setText(jTextArea1.getText()+"\n"+cursor.next());
System.out.println(cursor.next());
}
}
Maybe you need to do something like that...
while (cursor.hasNext()){
BasicDBObject object = (BasicDBObject) cursor.next();
String name = object.getString("name_of_column");
jTextArea1.setText(name);
https://www.baeldung.com/java-mongodb
mongodb - java get field value
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to firebase, I am using the Auth and Realtime Database services, so in my database I have two nodes- "User" and "Owner",
Database Snapshot
When the user registers using the auth, their data is put in either of these nodes (which is done using activities, there's a radio button for people to select if they are user or owner and proceed with the activity depending on their choice),
Auth table snapshot
What I want to implement now, is a way to determine if the person who logs in using the credentials from firebase auth, belongs to the "users" node or the "owners" node, as I am new to this, please provide the code or a way to manipulate my database to facilitate this determination.
Add this to your activity's onStart() method:
private FirebaseAuth mAuth;
private FirebaseUser mUser;
private FirebaseDatabase mDatabase;
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance();
mDatabase.getReference("Owners").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
Map<String,Object> owners = (Map)snapshot.getValue();
if(owners.containsKey(mUser.getUid())){
//Owner code
}
else{
//User code
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
This is a way you can do it but it would be better if you rethink your database design to do it faster. Instead of having two nodes you could just add an attribute to the user (say 'role') to indicate his role instead of having two nodes seperately. Better have the user UID on the first level itself.
This question already has answers here:
Android Firebase retrieve data from child node
(5 answers)
Closed 4 years ago.
I am building an app that will be able to let you answer assessment questions. Now I have a problem on how to save the scores to the current user. I cannot access the child node. This is the real time database. How can I access the child node under Users
You can simply use for loop to access the users just write this code under addOnEventListener:
DataSnapshot Users = datasnapshot.child("Users");
for(DataSnapshot userChild : Users.getChildren()){
DataSnapshot email = userChild.child("email");
String emailString = email.getValue(String.class);
}
I'm using push() method to create unique ids for database but when I create an id with push method its added to bottom of tree but I want it to be on top. How can I do that?
For example in this picture bottom one is newest one I want it to be on top. How can I do that?
Picture
I want this because I use RecyclerView with Firebase adapter and adapter start to read from top.
edit: how to get data from bottom to top from firebase?
This question is what I want but there is no useful answer.
I'm using push() method to create unique ids for database but When i create an id with push method its added to bottom of tree but i want it to be on top
When you are using the push() method, the objects in the Firebase Console are ordered chronologically and unfortunately there is nothing you can do about it. This order cannot be changed. If you want to display the items from the datababse in a RecyclerView and hypothetically speaking, you would have been able to change the order in the console, it doesn't mean that this order is the same with order in the RecyclerView. To display your items in a particular order, I recommend you to use a Query and sort them according to a timestamp property. For that, please see Frank van Puffelen's answer from this post and if you are using the Firebase-UI library, please see my answer from this post.
you can write a simple query to get data as you need it
add date parameter to your object then query the data by the date
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("data").orderByChild("date");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
// fill recyclarView with data
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
CONTEXT :
Hi, I'm currently working on an Android project backed by Firebase.
I have set up a denormalized data structure that relates polls to users (many-to-many relationship) via way of votes. Here is an image displaying the votes path of my database. The structure is as follows :
votes -> [pollUid] -> [votePushuid] -> vote object
So in this example we have a single poll that has 4 votes.
I want to run a check to see if a user has already voted on a poll. In order to do this I fetch the pollsUid, then run through its votes to see if any of them contain the voterUid property == to their user uid.
This is done as follows :
FirebaseHandler.getInstance().getMainDatabaseRef()
.child(FirebaseConstants.VOTES) //votes root
.child(pollKey) //polluid
.orderByChild("voterUid")
.equalTo(FirebaseHandler.getInstance().getUserUid())
.limitToFirst(1)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
If the datasnaptshot exists then we know that the user has already voted on this poll and can handle it in the Java logic.
PROBLEM :
The datasnapshot received by onDataChange is always null (ie does not exist) when searching for a specific user's vote on a specific poll. I know for a fact that the vote exists in the db through inspecting the data, and that the userUid is correct via debugging. Removing the equalTo and limitToFirst returns all of the votes for the poll without a problem so clearly the stem of the ref is correct. This implies to me that the issue is created by one of the two methods just mentioned. Even stranger is the fact that this approach does work at certain times, but not at others.
QUESTION :
How do I return a list of firebase stored objects filtered by a grandchild property? If this is not possible what would be the more appropriate datastructure for this problem?
On a further note I've seen people taking the approach of using Query instead of Databasereferences. Perhaps this might have something to do with the current issue.
Your query is correct. I have no problem running that query using my own DB. It's probably the userId doesn't match. DatabaseReference extends Query, that's why you can access Query's methods.
A database structure alternative would be
{ "users_votes": {
"<userId>": {
"<pollId1>" : true,
"<pollId2>" : true,
"<pollId3>" : true
}
}
}
Set the value to that node once the user voted to a poll.
To check if the user has voted for a poll
usersVotesRef.child(FirebaseHandler.getInstance().getUserUid())
.child(pollKey).addListenerForSingleValueEvent(valueEventListener);