Home > Software engineering >  Error: child failed: path argument was an invalid path = "users/[object Object]" using fir
Error: child failed: path argument was an invalid path = "users/[object Object]" using fir

Time:06-21

The issue is with the identityNumber variable. it returns error link 23 that string is needed. However my identity number is a string. PLease help. Also goal is to make identity number unique by appending a random int to a now Date().

import React, { useState } from "react";
import {initializeApp} from 'firebase/app';
import { getDatabase, ref, set, } from 'firebase/database';

export default function Username(props) {
        const [userName, setUsername] = useState('');
        const identityNumber = 'hello';

        const clientCredentials = {
            apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
            authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
            databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
            projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
            storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
            messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
            appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
        }
        const app = initializeApp(clientCredentials);
        const address = props.address;
        const db = getDatabase();

        function writeUserData(userId, userN, address) {
            const reference = ref(db, 'users/'   userId);

            set(reference, {
                username: userN,
                walletAddress: address
            });
        }

        function handleChange(event) {
            setUsername(event.target.value);
        }

        function handleClick() {
            writeUserData( { identityNumber }, { userName }, { address });
        }

        return (
            <>
                <div>
                    <p className = "account-Info" >address: {address}</p>
                </div>
                <div id="form">
                    <h2 className='user-Create' > Create Username</h2>
                    <form id='set-User'>
                        <input id='username' className="user-Create" type='text' onChange={handleChange} 
                        required minLength='3' maxLength='30' pattern="[a-zA-Z0-9_] " title='only letters, numbers, and underscores.'/>
                        <button className='user-Create' type="submit" onClick={handleClick}>Link Username</button>
                        <p id='call-User' className='account-Info'> { userName }</p>    
                    </form>
                </div>
            </>
        );
}

CodePudding user response:

Wrapping your variables in curly braces create literal objects, just don't:

function handleClick() {
    writeUserData(identityNumber, userName, address);
}
  • Related