0

I'm using Firebase to add auth to my site, and I want to export the user const into the express so I can pass it to a route. This is the function to create the user.

const signupForm = document.getElementById('signup');
if (signupForm) {
    signupForm.addEventListener('submit', (e)=>{
        e.preventDefault();
        const email = signupForm.email.value;
        const password = signupForm.password.value;
        createUserWithEmailAndPassword(auth, email, password)
        .then((cred)=>{
            return cred.user;
        })
        
        signupForm.reset();
    })
}

But when I require anything from the firebase file into express I keep getting this error

 const signupForm = document.getElementById('signup');
                    ^
 ReferenceError: document is not defined

I understand that the error is because document is DOM related and can't be run in express, but is there a way for me to export only the user const into express? any help is appreciated and thank you.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429/283366) – Phil Jan 19 '22 at 23:36
  • @Phil I'm not trying to pass HTML to the server. I'm trying to pass an object that has information about the user (email, Id...) so I can render an EJS temple with that information. This error occurs just because I have "Document" in the first file. – Abdallah Bari Jan 20 '22 at 00:09

1 Answers1

0

What you need to do is to send a POST request to the route, because the express server and the web page you load are completely separeted.

Add a fetch request like this:

            fetch('/api/users', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(cred.user)
            }

And the you handle the data in express.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thank you this worked. I used Axios to send the Post request and bodyParser.JSON to parse the user data in the express rout – Abdallah Bari Jan 20 '22 at 14:38