0

I am a newbie in cloud functions. I am trying to access my values. But there are 2 problems. First of all, When I am trying to run my function ( also, My other functions have this problem) My console gives me the same results that ran before.

Such as:

The first function = Gives me an apple

I run it.

The second function = Gives me a banana.

I run it.

Result: it gives me an apple. After that I am starting again my function that's time gives me a banana.

How can I fix it?

Also, Here is my main question:

 exports.scheduleremindernotifications  = functions.database.ref('/users/{userId}/name').onCreate((snapshot, context) => {
    
    
    const usersRef = admin.database().ref('users');
    usersRef.once('value', (snapshot) => {
      snapshot.forEach((childSnapshot) => {
        const userId = childSnapshot.key;
        const consumingRef = admin.database().ref(`users/${userId}/missions`);
        consumingRef.once('value', (snapshot) => {
          snapshot.forEach((childSnapshot) => {
    const missions_keys = childSnapshot.key;
    
    // Start at time elde ettik.
    const startatref = admin.database().ref(`users/${userId}/missions/${missions_keys}/startat`);
    
    
       startatref.once('value', (snapshot) => {
    snapshot.forEach((childSnapshot) => {
    const notification_time = childSnapshot.val;
    

// I want to access all my startat' values. And I want to show them in my console.
    
console.log(`${notification_time}`);
     
    
    

    
    
    });
    
    });

Also here is my realtime database structure:

enter image description here

How can I show my values? I want to access "startat" values. And showing to my console. How can I fix it?

  • Can you explain in more details what is your goal? It is not clear why you trigger the function on the `'/users/{userId}/name'`path (so when a user node is created with a `name` subnode) and then loop over ALL the users in your database. – Renaud Tarnec May 06 '23 at 07:43
  • İt is not necessary. I am only using it for the trigger to my console. this path : '/users/{userId}/name. I am testing now. I will split it. Also, Did you find any solution to database bug? İt is still show me same values. ( I'm talking about the first problem that I mentioned in the question.) – Berkay Isıkoglu May 06 '23 at 07:44
  • Did you solve your problem (since you created your own answer)? – Renaud Tarnec May 06 '23 at 07:45
  • Yes. This is a miracle. I am working 9 hours. But I am not still find the solution my first problem. – Berkay Isıkoglu May 06 '23 at 07:47
  • Your first problem described with banana and apple is not crystal clear. But it is most probably due to the way properties are ordered, see for example https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Renaud Tarnec May 06 '23 at 07:52
  • No. You do not get it. My problem is about Cloud functions. Not about working. It is working. But it is wrong. My server always works wrong. It runs always wrong functions. That functions: I wrote it before. – Berkay Isıkoglu May 06 '23 at 07:56
  • I am not sure what the actual question is but (loosely speaking) Firebase data can be unordered, so if a node is queried 3 times, you could get data in three different orders. Adding an `orderedBy` clause guarantees ordering. Does that help? – Jay May 06 '23 at 12:30

1 Answers1

0

Just add to your code these.

startatref.once('value').then(allToken => {
if(allToken.val()){
const token = allToken.val.toString();

console.log(`token? ${token}`);

}

});