Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: child failed: path argument was an invalid path = "users/[object Object]" using firebase and next.js

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>
            </>
        );
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading