Home > Mobile >  Firebase Realtime Database, setting data keys (without including a value)
Firebase Realtime Database, setting data keys (without including a value)

Time:01-16

I'm currently able to set data to my Firebase Realtime Database, it's being structured as seen below.

enter image description here

However, I want to set my data so that it's only displaying "users" and the uid nested underneath. I DON'T want to include the third nested data "uid: "yzQrwWv..."

How can I accomplish structing the data like this? My code:

import { createSlice } from "@reduxjs/toolkit";
import { database, auth } from "../components/firebase-config";
import { ref, set } from "firebase/database";

const initialState = {
  value: 0,
};

export const counterSlice = createSlice({
  name: "test",
  initialState,
  reducers: {
    tester: () => {
      const user = auth.currentUser;
      const uid = user.uid;

      set(ref(database, `users/${uid}`), {
        uid,
      });
    },
  },
});

// Action creators are generated for each case reducer function
export const { tester } =
  counterSlice.actions;

export default counterSlice.reducer;

I thought the following would work, but nothing is being displayed in my database:

     ref(database, `users/${uid}`);

CodePudding user response:

There is no way to have a key in the Firebase Realtime Database without a value under it. If you try to do so, Firebase will automatically delete the key.

The idiomatic way to have the key without a meaningful value, is to set true as the value. In your case that'd be:

set(ref(database, `users/${uid}`), true);

Or:

set(ref(database, `users`), {
  [uid]: true
});
  •  Tags:  
  • Related