使用 javascript 将文档添加到 Firestore
Adding documents to firestore using javascript
我正在尝试创建一个函数来将文档添加到 firestore。目前这个函数可以用两种不同的方式调用。 1) 将用户添加到名为 'Users' 的 collection。 2) 将站点添加到名为 'Sites'.
的 collection
此函数应采用以下参数:
名称collection(必填)
用户 uid(可选)
带有站点数据的 object(将其设为可选)
我也在尝试利用 JS 模块来更好地组织我的代码。目前我的文件夹看起来像这样:
myApp
assets
img
dist
index.html
index.js
modules
auth.js
firestore.js
里面 auth.js 我有一个 signUp() 里面 我想调用一个叫做 addToFirestore() 的函数 (这来自 firestore.js)。我的代码看起来像这样:
firestore.js
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.4/firebase-
app.js';
import { getFirestore, setDoc, doc } from
'https://www.gstatic.com/firebasejs/9.6.4/firebase-firestore.js';
const firebaseConfig = {
...
};
// Initialize Firebase
initializeApp(firebaseConfig);
const db = getFirestore;
function addToFirestore(collName, user = 0, data = 0) {
// check if adding user
if (user != 0 && data == 0){
//adding user to firestore
try {
setDoc(doc(db, collName, user.uid), {
email: user.email,
});
} catch (e) {
console.error('Error adding document: ', e);
}
// check if adding site
} else if (data != 0 && user == 0) {
setDoc(doc(db, collName), data);
}
export { addToFirestore};
在auth.js中这样调用函数:
// * Auth
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from
'https://www.gstatic.com/firebasejs/9.6.4/firebase-auth.js';
import {addToFirestore} from '/modules/firestore.js';
function signUp(email, password) {
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
addToFirestore('Users', user);
})
.then(() => {
openApp();
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alertAuth.classList.remove('d-none');
alertAuth.classList.add('show');
alertAuth.innerHTML = `<strong>Error: </strong> ${errorCode}`;
});
}
并在 index.js 中调用 signUp():
btnAuthSignUp.addEventListener('click', function () {
event.preventDefault();
let email = inpAuthEmail.value;
let password = inpAuthPassword.value;
signUp(email, password);
});
它给我这样的错误:
firestore.js:31 Error adding document: FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
我尝试在一个文件中完成所有操作,但仍然遇到同样的问题。任何帮助将不胜感激。
如您遇到的错误所述:
Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore.
Firestore 需要一个 collection 引用来传递数据。您没有指定 collection 参考。您应该使用下面给定的代码来放置 collection 引用:
const collectionRef = doc(db, 'collection_name', user.uid);
db
这里不是 collection 参考。它只是 Firestore 的一个实例:
const db = getFirestore;
然后将其用作下面的代码:
setDoc(collectionRef, {
email: user.email,
});
您还可以查看 Add data to Cloud Firestore 了解更多信息。
我正在尝试创建一个函数来将文档添加到 firestore。目前这个函数可以用两种不同的方式调用。 1) 将用户添加到名为 'Users' 的 collection。 2) 将站点添加到名为 'Sites'.
的 collection此函数应采用以下参数:
名称collection(必填)
用户 uid(可选)
带有站点数据的 object(将其设为可选)
我也在尝试利用 JS 模块来更好地组织我的代码。目前我的文件夹看起来像这样:
myApp
assets
img
dist
index.html
index.js
modules
auth.js
firestore.js
里面 auth.js 我有一个 signUp() 里面 我想调用一个叫做 addToFirestore() 的函数 (这来自 firestore.js)。我的代码看起来像这样:
firestore.js
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.4/firebase-
app.js';
import { getFirestore, setDoc, doc } from
'https://www.gstatic.com/firebasejs/9.6.4/firebase-firestore.js';
const firebaseConfig = {
...
};
// Initialize Firebase
initializeApp(firebaseConfig);
const db = getFirestore;
function addToFirestore(collName, user = 0, data = 0) {
// check if adding user
if (user != 0 && data == 0){
//adding user to firestore
try {
setDoc(doc(db, collName, user.uid), {
email: user.email,
});
} catch (e) {
console.error('Error adding document: ', e);
}
// check if adding site
} else if (data != 0 && user == 0) {
setDoc(doc(db, collName), data);
}
export { addToFirestore};
在auth.js中这样调用函数:
// * Auth
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from
'https://www.gstatic.com/firebasejs/9.6.4/firebase-auth.js';
import {addToFirestore} from '/modules/firestore.js';
function signUp(email, password) {
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
addToFirestore('Users', user);
})
.then(() => {
openApp();
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alertAuth.classList.remove('d-none');
alertAuth.classList.add('show');
alertAuth.innerHTML = `<strong>Error: </strong> ${errorCode}`;
});
}
并在 index.js 中调用 signUp():
btnAuthSignUp.addEventListener('click', function () {
event.preventDefault();
let email = inpAuthEmail.value;
let password = inpAuthPassword.value;
signUp(email, password);
});
它给我这样的错误:
firestore.js:31 Error adding document: FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
我尝试在一个文件中完成所有操作,但仍然遇到同样的问题。任何帮助将不胜感激。
如您遇到的错误所述:
Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore.
Firestore 需要一个 collection 引用来传递数据。您没有指定 collection 参考。您应该使用下面给定的代码来放置 collection 引用:
const collectionRef = doc(db, 'collection_name', user.uid);
db
这里不是 collection 参考。它只是 Firestore 的一个实例:
const db = getFirestore;
然后将其用作下面的代码:
setDoc(collectionRef, {
email: user.email,
});
您还可以查看 Add data to Cloud Firestore 了解更多信息。