I'm encountering an issue with my API while trying to utilize Firestore for inputting data for login and registration via the API. The problem arises when attempting to add a document entry in the database with the user's input email during registration, resulting in an error. My objective for adding a document with the user's email is to simplify the subsequent login step by facilitating validation to compare both the password and email.
const functions = require("firebase-functions");
const express = require("express");
// eslint-disable-next-line no-unused-vars
const cors = require("cors");
const admin = require("firebase-admin");
admin.initializeApp();
const app = express();
const db = admin.firestore();
app.post("/Register", (req, res) => {
const {
name,
email,
password,
phone,
} = req.body;
const saldo = 0;
const valAcc = db.collection("Users").doc(email);
valAcc.get().then((doc) => {
if (doc.exists) {
res.status(409).send("User Already Exist!");
}
});
db.collection("Users").doc(email).add({
Name: name,
Email: email,
Password: password,
Phone: phone,
Saldo: saldo,
});
res.status(201).send();
});
exports.apptest = functions.https.onRequest(app);
Does anyone have a solution or suggestion? Any help would be greatly appreciated.