I have a data structure that looks like this:
users > {user-auto-id} > network > {network-auto-id}
I m not sure how I can add entries to the network. I m updating a document of users like the following which is working fine
const captureCol = collection(
firestore,
'users'
);
const docToUpdate = doc(myCol, 'id123');
const udata: any = {
title: 'title',
};
if (data.testExecutionLinkData) {
delete udata.testExecutionLinkData;
}
await updateDoc(docToUpdate, udata);
I tried to get the reference of the nested document like this, but it's failing
docToUpdate.collection('network1'); // ERROR: collection is not a function
You're getting an error when using:
docToUpdate.collection('network1');
Because you're creating a collection reference and not a document reference. To solve this, you have to create a reference that points exactly to the document you need to be updated. In code it should look like this:
db.collection('users').doc('user-auto-id').collection('network').doc('network-auto-id')
And right after that perform the update.
Related
I am using a scheduled task in a Firebase Cloud Function to query an array which contains a number of objects that need to be updated if a matching condition exists. My current attempt is using the 'array-contains' method to get the objects, then loop over them to find a matching condition which will then batch update the items. This is my data structure:
I need to find an object that is <= the current time, and also if the 'active' value = false.
export const liveMeetingsTrigger = functions.runWith( { memory: '1GB' }).pubsub
.schedule('every 1 minutes').onRun(async context => {
const now = admin.firestore.Timestamp.now();
const liveMeetings = await admin.firestore().collection('fl_content').where('meeting', 'array-contains', 'liveMeetingDate').get();
const batch = admin.firestore().batch();
liveMeetings.forEach(doc => {
if(doc.data().liveMeetingDate <= now && doc.data().active == false){
batch.update(doc.ref,'active',true);
}
});
return await batch.commit();
});
I have also tried using an exact object in the query instead of just using 'liveMeetingDate', but still get no results back, any help would be great - thanks.
Debugging: As the array I am trying to reach is inside of the (map) object 'liveMeetings' i have tried the dot notation (liveMeetings.meeting) with no success. Also trying a new collection with the the 'meeting' array at top level has provided no success.
Simple logging in the console (liveMeetings.size) shows that nothing is being returned on the query, so therefore the logging does not even reach the loop in the code.
As explained in this anwser the following query will not work:
const liveMeetings = await admin.firestore().collection('fl_content').where('meeting', 'array-contains', 'liveMeetingDate').get();
because the meetings array contain some objects, instead of "simple" or primitive data (e.g. string, number...).
You could query it with the exact objects, like:
const obj = {active: false, liveMeetingDate: ..., meetingId: ..., ....};
const liveMeetings = await admin.firestore().collection('fl_content').where('meeting', 'array-contains', 'obj').get();
Another approach would be to create a new collection which contains the similar documents (same Document ID) but with a meeting Array that contains only the liveMeetingDate property.
Finally, note that since your Array is within a map, you need to do
await admin.firestore().collection('fl_content').where('liveMeetings.meeting', 'array-contains', ...).get();
(PS: I don't mark this question as duplicate since you expressly ask for more help in the comments of the duplicate question/answer)
I have not been able to find a reference in the documentation on how to get a document reference when you know the id of it in firebase.
I am passing the id to a webpage to lookup a QR code. Rather than storing a secondary unique id for each qrcode document I am relying on the firebase unique id.
Here is the lookup I tried but which seems to fail.
firebase.firestore().collection('cues').doc(id).get().then(function (docsnapshot) {
console.info('About: ' + docsnapshot.get('text'));
});
I was able to get my original code to work with this modification to the query
firebase.firestore().collection('cues').doc(id).get().then((doc) => {
... and then just use doc.get("field") to get values form my document
you can access to data like this:
const { id } = docsnapshot
const data = docsnapshot.data()
const myDoc = { id, ...data }
myDoc.text
myDoc.anything...
I try to download and show only specific data from the Realtime Database. I have the following code:
getUserPlatformIos() {
this.dataRef = this.afDatabase.list('data/users', ref => ref.orderByChild('meta/platform').equalTo('ios'));
this.data = this.dataRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
return this.data;
}
My firebase database structure
Firebase rules
Why firebase does download the whole database if I query before? This causes very long loading times and a lot of downloaded data....
Indexes need to be defined at the place where you the query. Since you run the query on data/users, that's where you need to define your index:
"users": {
".indexOn": "meta/platform"
}
This defines an index on users, which has the value of the meta/platform property of each user.
Note that the log output of your app should be showing an error message with precisely this information. I highly recommend checking log output whenever something doesn't work the way you expect it to work.
Is it possible to get the data you just saved back from Firebase via AngularFire2?
I want to have the saved data with it's generated UUID.
I'm saving the data like this:
const phase = this.fireStore.collection("pr").doc(prId).collection("col1").doc(phase)
phase.collection("content").add({"content": post.content})
Now I want to return the saved data back as an Observable that has a map for my custom object.
What I now have is that I first convert it to an Observable and then I do the get via valueChanges.
To me this seems like overkill.
I'm getting it done like this:
const phase = this.fireStore.collection("projects").doc(post.projectId).collection("canvas1").doc(post.phase)
return from(phase.collection("content").add({ "content": post.content })).pipe(map(ref => {
return new Post(ref.id, post.content)
}))
if someone knows a better way please share :-)
Im new to Firebase and am having trouble access some nested JSON data. Here is my data structure:
In Samples I have a set of JSON data which I am trying to access. Previously I had the database setup so that there was no "users" subcategory and all the samples JSON data was in the root and the data was loading fine like this. Since creating parent categories i've had trouble trying to access the Samples data. Here is how i'm trying to reach it (using React):
componentWillMount() {
var db = firebase.database();
var ref = db.ref('music-app-7a4d3').child("samples");
ref.once('value')
.then((snapshot) => {
var samplesObject = snapshot.val();
this.setState({
samples: samplesObject
});
});
},
The componentWillMount function basically sets the initial state of the app with the objects found in samples. I'm a little bit lost as I thought I could access the next tier down by using the .child method but its not finding the data. Where am I going wrong here?
As requested heres an expanded view of my data:
Try this:
var ref = db.ref('music-app-7a4d3/samples/your/awesome/subitem');
EDIT:
try remove the music-app-7a4d3 keep only this: /samples/your/awesome/subitem