Alperen ARICI
Alperen ARICI

Reputation: 213

Firebase real-time database deletes old data and replaces it

I want to add a new data on my real time database. However Database deletes old data and write new data. My data scheme like that :

enter image description here

And I want to add a new user on rooms/roomid/users/

My code part :

joinRoom = async (callback = f => f, roomId) => {
        
        currentRoomUid = roomId
        var username = ''
        await database().ref(`/users/${auth().currentUser.uid}`).once('value', function (snap) {
            username = snap.val().username
        })

        const room = database().ref(`/rooms/${roomId}`);
        await room.set({
            isStart: true,
            currentQuestionIndex: 0
        });

        const usersRef = database().ref(`/rooms/${roomId}/users`)
        usersRef.child(auth().currentUser.uid).set({
            username: username,
            skor: 0
        });

        callback(roomId)
    }

The important point here is where I add the user. I use the set method. How can I fix it ? I want to add the second user.

The Problem

Old Data

enter image description here

After adding operation

enter image description here

Upvotes: 0

Views: 774

Answers (2)

israel
israel

Reputation: 135

.set() method will delete your old data and will create new data. so when you using room.set, you deleting everything in room. that's include users. so every time user enters a room he will delete the previous user and add himself instead. you should use room.update like this:

const room = database().ref(`/rooms/${roomId}`);
    await room.update({
        isStart: true,
        currentQuestionIndex: 0
    });

if you want to know better the different between set and update read the answer for this question: Firebase update vs set

here is the full code you should use:

joinRoom = async (callback = f => f, roomId) => {

    currentRoomUid = roomId
    var username = ''
    await database().ref(`/users/${auth().currentUser.uid}`).once('value', function (snap) {
        username = snap.val().username
    })

    const room = database().ref(`/rooms/${roomId}`);
    await room.update({
        isStart: true,
        currentQuestionIndex: 0
    });

    const usersRef = database().ref(`/rooms/${roomId}/users`)
    usersRef.child(auth().currentUser.uid).set({
        username: username,
        skor: 0
    });

    callback(roomId)
}

Upvotes: 2

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

Update following your comments:

If you want to add different users under the rooms/423c34.../users node, you need to pass different values to the child() method. For example:

        const usersRef = database().ref(`/rooms/${roomId}/users`)
        await usersRef.child('userKey1').set({  
            username: username1,
            skor: 0
        });

        await usersRef.child('userKey2').set({   
            username: username2,
            skor: 0
        });

If I correctly understand your question, the following adaptations should do the trick:

joinRoom = async (callback = f => f, roomId) => {
        
        currentRoomUid = roomId
        var username = ''
        const snap = await database().ref(`/users/${auth().currentUser.uid}`).once('value');
        const username = snap.val().username;
    
        const room = database().ref(`/rooms/${roomId}`);
        await room.set({
            isStart: true,
            currentQuestionIndex: 0
        });

        const usersRef = database().ref(`/rooms/${roomId}/users`)
        await usersRef.child(auth().currentUser.uid).set({   //Note the await here
            username: username,
            skor: 0
        });

        callback(roomId)
    }

Note that you didn't await the last call to the set() method, so most probably the callback was executed before the new user was persisted to the Database.


Note also that you may use the update() method, to "perform simultaneous updates to multiple locations in the JSON tree with a single call".

Upvotes: 2

Related Questions