Content-Length
[Firebase]ドキュメント作成
ドキュメント作成
//コピー元ドキュメント取得
cont searchDoc;
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.where('testfield', '==', 'xxxxxxxxxxxxxxxxxxxx')
.get()
.then(snapshot => {
snapshot.docs.forEach(doc => {
searchDoc = doc.data();
});
});
//取得したドキュメントをコピーしてドキュメント作成
let insertDoc = Object.assign({}, searchDoc);
//ドキュメントのfield変更
insertDoc.testfield = 'testfield';
insertDoc.testfield2 = 'testfield2';
//ドキュメント作成
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.add(insertDoc);
[Firebase]コレクションのドキュメント全件取得
コレクションのドキュメント全件取得
//コレクションのドキュメント全件取得
var docData = [];
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.get()
.then((doc) => {
if (doc.docs) {
doc.docs.map((d) => {
let data = d.data();
docData.push(data);
});
}
else{
console.log('none');
}
});
[Firebase]ドキュメントId取得
ドキュメントId取得
//testfieldが該当の値のtestcollのドキュメントID取得
const documentId;
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.where('testfield', '==', '1234xxxxxxxxxxxxx5678')
.get()
.then(snapshot => {
snapshot.docs.forEach(doc => {
documentId = doc.id;
});
});
[Firebase]ドキュメント更新
ドキュメント更新
//testfieldの値の更新
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.doc("xxxxxxxxxxxxxxxxxxxx")
.update({
testfield: "testName"
});
[Firebase]ドキュメント削除
ドキュメント削除
//testfieldが該当の値のtestcollのドキュメントID取得
const documentId;
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.where('testfield', '==', '1234xxxxxxxxxxxxx5678')
.get()
.then(snapshot => {
snapshot.docs.forEach(doc => {
documentId = doc.id;
});
});
//testcollのドキュメントIDのドキュメント削除
try{
await firebase.firestore()
.collection('testcolloya')
.doc('testdocoya')
.collection("testcoll")
.doc(documentId)
.delete();
}catch(e){
console.log(e);
}
[Reference url]Firestoreでidのわからないdocumentを取得したいときはどうすればいいのか
Firestoreでidのわからないdocumentを取得したいときはどうすればいいのか
[Firebase]Getting all documents from one collection in Firestore
Getting all documents from one collection in Firestore
const events = await firebase.firestore().collection('users')
events.get().then((querySnapshot) => {
const tempDoc = []
querySnapshot.forEach((doc) => {
var data = doc.data();
console.log(data);
tempDoc.push(data);
})
console.log(tempDoc);
})
[Firebase]匿名認証サンプルコード
匿名認証サンプルコード
const firebaseConfig = {
apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
authDomain: "sample.firebaseapp.com",
databaseURL: "https://sample.firebaseio.com",
projectId: "sample",
storageBucket: "sample.appspot.com",
messagingSenderId: "XXXXXXXXX",
appId: "1:XXXXXXX:web:XXXXX"
};
const app = firebase.initializeApp(firebaseConfig);
await firebase.auth().signInAnonymously().catch(e => {
console.log(e)
})
[Firebase]auth/admin-restricted-operation This operation is restricted to administrators only.エラー対応
Firebaseで認証時に以下のエラーの対応
auth/admin-restricted-operation This operation is restricted to administrators only.
Firebaseのコンソール画面 > Authentication
> Sign-in method
> 利用したい認証方式有効