I am stuck on probably a very basic thing, but I can't seem to get the data from Firestore into the return variable "profile"
What am I doing wrong when looking at the code below?
let profile;
export const getWidgets = createAsyncThunk(
"projectDashboardApp/widgets/getWidgets",
async () => {
var docRef = db().collection("companies").doc("cmpn0000001");
docRef
.get()
.then((doc) => {
if (doc.exists) {
profile = doc.data()
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
})
.catch((error) => {
console.log("Error getting document:", error);
});
return profile;
}
);
Related
I have my function on my class and i have to catch my subcollection. everything works fine with my collection (my console.log is working) but when i tried to catch something on my subcollection i have this error :
TypeError: undefined is not a function (near '...(0, _firestore.default)().collection("Teams").doc(docid).collection("membersList").where("statut", "==", "Validé").then...')
also my code :
GetMembersTeam = async () => {
firestore()
.collection("Teams")
.where("tokenTeam", "==", "c2z3f4vb3bh")
.get()
.then((querySnapshot) => {
if (querySnapshot.empty) {
console.log("no documents found");
} else {
querySnapshot.forEach((doc) => {
let Teams = doc._data;
let docid = doc.id;
firestore()
.collection("Teams")
.doc(docid)
.collection("membersList")
.where("statut", "==", "Validé")
.then(async (querySnapshot) => {
if (querySnapshot.empty) {
console.log("no documents found");
} else {
querySnapshot.forEach(async (doc) => {
let Teams = doc._data;
let docid = doc.id;
console.log(docid);
});
}
});
});
}
});
};
componentDidMount() {
this.GetViewTeam();
this.GetMembersTeam();
}
Are you missing a get()?
firestore()
.collection("Teams")
.doc(docid)
.collection("membersList")
.where("statut", "==", "Validé")
.get() //<- are you missing a get?
.then(async (querySnapshot) => {
if (querySnapshot.empty) {
console.log("no documents found");
} else {
querySnapshot.forEach(async (doc) => {
let Teams = doc._data;
let docid = doc.id;
console.log(docid);
});
}
I'm making an app and trying to get product data by it's id inside a modal in ionic 4.
I'm using typescript to do it but without luck.
Because de calls to firebase are asynchronous i cannot get the data that is held in firebase and also because i'm new to subject i cannot figured out the proper way to write the code.
I read about how to do it but i'm having a hard time to achieve it.
Here is my function that tries to grab product data from firebase.
It always logs empty on console.log('todo', todo).
async editProduct(id) {
const getTodo = docRef => {
setTimeout(() => {
docRef = this.afs.collection("products").doc(id);
docRef.get().subscribe((doc) => {
if (doc.exists) {
let data = doc.data();
return data;
} else {
console.log("No document.");
return false;
}
});
}, 2000)
}
getTodo(todo => {
console.log('todo', todo)
})
const modal = await this.modalCtrl.create({
component: AdminProductPage,
'id': id,
});
await modal.present();
}
There is something wrong with your "getTodo". Probable you are logging empty data with your code, I can give you the proper functional example:
myData
editProduct() {
this.afs.collection("products").doc(id)
.valueChanges()
.subscribe(data => {
console.log(data)
myData = data
})
}
getData() {
console.log(this.myData) // You will log it twice with this line
}
GOOGLE EXAMPLE
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
https://firebase.google.com/docs/firestore/query-data/get-data?hl=es
I'm working in Reactjs and my database is Firebase.
I'm trying to retrieve data from my database and then checking data regarding it.
At the top, I have const [group, setGroup] = useState(null); for my group data. In my CheckGroupRelationship function, if checks the database using the group object data.
Initially I had the function called after the database call:
const GetGroupInfo = async (uid) => {
await props.firestore
.collection("groups")
.doc(uid)
.get()
.then(async (doc) => {
if (!doc.exists) {
console.log("No such document!");
} else {
setGroup({
id: doc.data().id,
ownerID: doc.data().ownerID,
name: doc.data().name,
});
}
})
.catch((err) => {
console.log("Error getting document", err);
});
CheckGroupRelationship();
};
However, I would get the error "Group is null". So I moved the function called to the setState:
const GetGroupInfo = async (id) => {
await props.firestore
.collection("groups")
.doc(id)
.get()
.then(async (doc) => {
if (!doc.exists) {
console.log("No such document!");
} else {
setGroup(
{
id: doc.data().id,
ownerID: doc.data().ownerID,
name: doc.data().name,
bio: doc.data().bio,
},
CheckGroupRelationship()
);
}
})
.catch((err) => {
console.log("Error getting document", err);
});
};
This would still result in the "group is null" error. I'm not sure what else I can do to get the function to be called AFTER the state is set.
the easy solution would be to use useEffect. In your case you can do this:
const [group, setGroup] = useState(null);
useEffect(() => {
if (group) {
CheckGroupRelationship()
}
}, [group])
What useEffect does here is waiting for group to change and when it changes it calls the inside callback. In this case you will have group updated there. Hope it helps, cheers
I have this query where I am trying to get a document from a sub-collection but it doesnt get anything, neither it shows any error. Could it be an issue in Firebase side or am I doing something wrong here:
const db = firebase.firestore();
let ref = db.collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2').collection('privat').doc('privat');
ref.get()
.then(doc => { //it doesnt continue to next line, just executes this one
if (doc.exists) {
console.log("Doc exists:", doc.data())
}else{
console.log("Doc deostn exists:")
}
}).catch(error => {
console.log('Error getting restaurant data: ', error);
})
However, if I try to query only a document from the collection then it works:
let ref = db().collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2');
EDIT: Screenshot of my database added
Change this:
const db = firebase.firestore.
let ref = db().collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2').collection('privat').doc('privat');
ref.get()
.then(doc => { //it doesnt continue to next line, just executes this one
if (doc.exists) {
console.log("Doc exists:", doc.data())
}else{
console.log("Doc deostn exists:")
}
}).catch(error => {
console.log('Error getting restaurant data: ', error);
})
Into this:
const db = firebase.firestore();
let ref = db.collection('myCollection').doc('EzGNfNl63LQvjdauFWosG08Ishj2').collection('privat').doc('privat');
ref.get()
.then(doc => { //it doesnt continue to next line, just executes this one
if (doc.exists) {
console.log("Doc exists:", doc.data())
}else{
console.log("Doc deostn exists:")
}
}).catch(error => {
console.log('Error getting restaurant data: ', error);
})
firestore() is a method and db is a constant.
What I want to achieve is to check if a document exist based on a field like liker_id and if it exist then return and if not then create a new document with the liker_id.
I've tried both snapshot.exists and doc.exists but they seem to give weird behaviors where the console.log doesn't get triggered or collection still adds document even though it already exists.
const liker = db
.collection("post")
.doc('ubxL0nDCLHgrtEyQ5TEV')
.collection("votes")
.where("liker_id", "==", "user-TVe8mMRU47cnfO4xbl9yQEEE4XB3")
.get()
.then(snapshot => {
snapshot.forEach(doc => {
if (doc.exists) {
console.log("exist");
} else {
console.log("not exist");
}
});
})
.catch(error => {
console.log(error);
});
Would you try the code like this?
const liker = db
.collection("post")
.doc('ubxL0nDCLHgrtEyQ5TEV')
.collection("votes")
.where("liker_id", "==", "user-TVe8mMRU47cnfO4xbl9yQEEE4XB3")
.get()
.then(snapshot => {
const data = snapshot.val();
data.forEach(doc => {
if (doc.exists) {
console.log("exist");
} else {
console.log("not exist");
}
});
})
.catch(error => {
console.log(error);
});