I'm trying to send the UserID of a created user to my database. I want to then reference data inside of the userID in the database.
In regards to the issue, I'm unaware of how L2GIZ7Sx-e-AALSJp4g and the others below were generated. The UserIDs are then attached to this identifier.
firebase.auth().onAuthStateChanged(User => {
console.log("user id: " + firebase.auth().currentUser.uid);
var UserID = firebase.auth().currentUser.uid;
var ref = firebase.database().ref("Users").push(UserID);
console.log("user logged in");
alert("You have been signed in");
});
I would like the data to be stored as,
+Users
+userid
data about user
Thank you.
When you call push() on a location, Firebase generates a unique key under that location (the values starting with -L in your screenshot).
If you want to instead determine the name of the child node yourself, you should use child(...) instead of push():
firebase.auth().onAuthStateChanged(User => {
console.log("user id: " + firebase.auth().currentUser.uid);
var UserID = firebase.auth().currentUser.uid;
var ref = firebase.database().ref("Users").child(UserID).set(true);
console.log("user logged in");
alert("You have been signed in");
});
The above sets the value of the node to true, since Firebase won't store a node without a value.
Related
I've been trying for a project I'm working on to develop a function for a Food chatbot. What I'm currently working on is to perform a method for a user to make a purchase of an order that is stored in firebase realtime database.
The method is set as the method for an actionMap and the actionMap is linked to an intent for knowing when to call the method and for retrieving the parameters.
My current method uses a simple check for a user's existence and status within the database before identifying the existence of the order they're trying to make a purchase for by its id by going through the user's reference path and doing a .forEach to check every order found and look at its parent folder name to check if it matches the user's order id. My code is as follows:
const MakePurchaseACTION = 'Make Purchase';
function makePurchase(app){
let email = parameter.email;
let orderId = parameter.orderId;
var currDate = currDateGenerator();
var name = email.split(".com");
//Check if User exists first in database
var userRef = database.ref().child('user/' + name);
return userRef.once('value').then(function(snapshot) {
if (snapshot.exists()) {
let statusRetrieved = snapshot.child('Status').val();
//Check if user's status in database is signed in.
if (statusRetrieved == "Signed In") {
var orderRef = database.ref().child('order/' + name);
//Check the order table for the user.
return orderRef.once('value').then(function(orderSnapshot){
let orderVal = orderSnapshot.val();
console.log(orderVal);
//Check through every child for the matching id.
orderSnapshot.forEach(function(childSnapshot) {
let orderIdFound = childSnapshot.key;
//let cost = childSnapshot.child('Cost').val();
console.log(orderIdFound);
if(orderId == orderIdFound) {
let eateryName = childSnapshot.child('Eatery').val();
let eateryLocation = childSnapshot.child('EateryLocation').val();
let deliveryAddress = childSnapshot.child('DeliveryAddress').val();
let orderItem = childSnapshot.child('OrderItem').val();
let quantity = childSnapshot.child('Quantity').val();
let cost = childSnapshot.child('Cost').val();
var purchaseRef = database.ref().child('purchase/' + name + "/" + currDate + "/" + orderId);
purchaseRef.set({
"Eatery" : eateryName,
"EateryLocation" : eateryLocation,
"DeliveryAddress": deliveryAddress,
"OrderItem" : orderItem,
"Quantity": quantity,
"Cost": cost,
"DateCreated": currDate
});
app.add("You have successfully purchased Order " + orderId);
} else {
app.add("There is no order with that id.");
}
});
});
} else {
app.add("You need to be signed in before you can order!");
}
}
else {
app.add("Sorry pal you don't exist in the database.");
}
});
}
actionMap.set(MakePurchaseACTION, makePurchase);
After checking through some firebase logs
Firebase Logs screenshot here
Firebase Realtime Database Order Table Sample
I found that the method actually completes Purchase table sample but my dialogflow returns with the stated error of:
Error: No responses defined for platform: undefined and displays "Not Available" back to the user. My question is how do I go about resolving this error?
I'm having trouble reading Firebase 'real database' data.
I created an intent with the parameter $nomeProf. When I pass a value and try to query in db a "Webhook call failed occurs.
Error: 500 Internal Server Error
Access to database is working, I just can't read the children.
I'm trying to get the email pass unique id in dialogflow consoles, example:
'qual o email do faria?', $nomeProf=faria.
'faria' and 'correa' are my unique like ID:
function getProfessorHandler (agent){
//let nomeProf = agent.parameters.nomeProf; #I need something like this?
return admin.database().ref("Professor").once('value').then((snapshot) => {
const value = snapshot.child("nomeProf").val();
const email = value.email;
if(value !== null){
agent.add(`O email é: ${email}`);
}
});
}
It sounds like you want to load a single child of Professor from the database, based on the agent.parameters.nomeProf value.
That'd be something like this:
return admin.database().ref("Professor").child(agent.parameters.nomeProf).once('value').then((snapshot) => {
const value = snapshot.val();
const email = value.email;
if(value !== null){
agent.add(`O email é: ${email}`);
}
});
I am trying to create a new entry (with userid) in my firebase database as soon as a user removes my app. (Qndroid)
'use strict';
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
exports.appremoved = functions.analytics.event('app_remove').onLog(event => {
console.log("App remove detected");
var user = admin.auth().currentUser;
var uid;
const uid2 = event.data.user.userId;
console.log("App remove detected2:" + uid2);
if (user != null) {
uid = user.uid;
console.log("User object not empty" );
admin.database().ref('/user_events/'+uid + "/"+Date.now()).set("app_remove");
}
});
I created two variables to get the user id, but both of them are undefined.
uid2 is undefined, and user is null.
I also tried
const user = event.data.user;
But it is also null
How do I get the userid of the user that removed the app?
Calling admin.auth().currentUser gets you the administrative user that is running the admin SDK. This user does not have a UID. And even if they did, it wouldn't be the user that removed the app.
Calling event.data.user.userId gives you the user ID that your app may have set via the setUserId API. If you didn't set a UserId, the call won't give you any value.
Using Firebase, I am trying to display a user's profile information based on their user id. I can currently save data to the database under the users ID.
I am able to display the general information from hobbies, but when altering the variables to display the user's data the id is then null or uid undefined and i can't figure out why? The user is signed in, and can upload data using their id.
//Display Profile Information
(function(){
var user = firebase.auth().currentUser;
var uid;
if (user != null) {
uid = user.uid;
}
const preObject = document.getElementById('object');
const list = document.getElementById('list');
//Displays Hobbies correctly, When changing 'object' & 'hobbies'
//to 'Users' & uid nothing is shown.
const dbRefObject = firebase.database().ref().child('object');
const dbRefList = dbRefObject.child('hobbies');
dbRefObject.on('value', snap=> {
preObject.innerText = JSON.stringify(snap.val(), null, 3);
});
dbRefList.on('child_added', snap => console.log(snap.val()));
};
Alterations:
const dbRefObject = firebase.database().ref().child('Users');
const dbRefList = dbRefObject.child(uid);
I am extremely grateful for any help or advice!
It looks like you are getting a value for dbRefObject which is pointed to the entire Users object and not a specific user's UID.
Try changing this:
dbRefObject.on('value', snap=> {
preObject.innerText = JSON.stringify(snap.val(), null, 3);
});
to this (dbRefObject changes to dbRefList):
dbRefList.on('value', snap=> {
preObject.innerText = JSON.stringify(snap.val(), null, 3);
});
I have an object called Group, which stores an array of Users (default Parse user) as a column. I am trying to list all of these users' display names (column called "displayName") in a certain group, but for some reason, when I try to use the .get function on a user in the retrieved array, it only gives me the information for the current user. I checked my permissions and ACL and it says for each user Public Read, and the User class has public read and write permissions. Here is the code I am using:
var groupObject = Parse.Object.extend("Group");
var users = [];
var groupQuery = new Parse.Query(groupObject);
groupQuery.get(groupId,{
success: function(group)
{
users = group.get("Users");
for(var i=0;i<users.length;i++)
{
var user = users[i];
console.log("display name: " + user.get("displayName") + "username: " + user.get("username") + "id: " + user.id);
}
doneCallback(users);
},
error: function(object, error)
{
errorCallback(error);
}
});
I am able to console.log all of their ids, and the query is successful, but the only thing I can use get("column") on is the current user (which is part of the group users array).
I think you should use a Relation instead of an Array to store your users in the Group object. It will be way easier to add, remove, fetch and access your user objects via the relation. You also avoid some headache when the list of your users becomes large. Then you should be able to fetch all the users in any given group like this:
var groupObject = new Parse.Object("Group");
groupObject.set("objectId", yourGroupId);
var query = groupObject.relation("Users").query();
Parse.Cloud.useMasterKey();
query.find().then( function(users) {
_.each(users, function(user) {
console.log(user.get("displayName"));
});
});