'Why setDoc(doc(db, colref, id), {...}) not creat new document in firestore
I have looked up Firestore document and see this method to create new document with customized ID
import { doc, setDoc } from "firebase/firestore";
// Add a new document in collection "cities"
await setDoc(doc(db, "cities", "LA"), {
name: "Los Angeles",
state: "CA",
country: "USA"
});
However, when I implement as follow, it returns undefined : doc is not defined
createUserWithEmailAndPassword(auth, txtEmail.value, txtPassword.value).then((userCredential) => {
setDoc(doc(db, 'users', userCredential.user.uid), {
email: txtEmail.value,
name: "nickname",
password: txtPassword.value,
on_line: false,
creat_time: Timestamp.fromDate(new Date())
}).then((docref) => {
txtEmail.value = "";
txtPassword.value = "";
create_alert("success", userCredential.user + " sign up success");
});
})
complete file :
import { db, auth } from "./config";
import {collection, getDoc, addDoc, updateDoc, setDoc, Timestamp} from "firebase/firestore";
import {getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword} from "firebase/auth";
new_account = () => {
var txtEmail = document.getElementById('inputEmail');
var txtPassword = document.getElementById('inputPassword');
createUserWithEmailAndPassword(auth, txtEmail.value, txtPassword.value).then((userCredential) => {
console.log("User: " + txtEmail.value + " has succesfully sign up");
setDoc(doc(db, 'users', userCredential.user.uid), {
email: txtEmail.value,
name: "nickname",
password: txtPassword.value,
on_line: false,
creat_time: Timestamp.fromDate(new Date())
}).then((docref) => {
txtEmail.value = "";
txtPassword.value = "";
create_alert("success", userCredential.user + " sign up success");
});
Above is most of my file
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
