i want to ask something. why when im trying to get the reference firestore data, the value was print out like this. and i dont understand what this values mean, and how to use this.
{
_key: { path: { len: 2, offset: 0, segments: [Array] } },
converter: null,
firestore: { app: [FirebaseAppImpl], databaseId: [ie], settings: [Yc] },
type: "document",
}
how to get the reference data and return the proper data from firestore ?
this is what my references looks like:
When you get the value of a reference field, you get back a DocumentReference object. If you just want to get its path, like is shown in your console screenshot, you can read the path property of the DocumentReference.
If you want to get the data from the referenced document (so from the warehouse), you can call get() on the DocumentReference, same as you'd do for a DocumentReference that you'd have constructed in another way. For more examples of that, see the documentation on getting a document.
Related
I am trying to get a Vue.js computed property to do some reconfiguring of data and when I do it inside the computed property I get an error about side effects. I looked it up on here and found that using a method is the correct way of doing this. Now when trying to use a method it results in error messages in the console about it can't read the property of 'units' from selectedBuilding.
What I am trying to figure out is how to have it so the units array is populated with the correct data depending on the computed return value
Below is the code stripped down for the script section of my Vue.js file.
export default {
name: "Form",
data: () => ({
building: {},
buildings: [
{
"units": [ // Bunch of data in here that isn't important//],
"buildingID": "<an ID according to a custom format>",
}],
units: [],
}),
methods: {
formatUnits: function(selectedBuildingID) {
let selectedBuilding = this.buildings.find(building => building.buildingID === selectedBuildingID)
this.units = selectedBuilding.units
return true
}
},
computed: {
useExistingBuilding() {
if(this.building === 'New') {return true}
else {
this.formatUnits(this.building)
return false
}
},
}
};
</script>
Error message:
TypeError: Cannot read property 'units' of undefined
Since this.building has no buildingID (thus, undefined), and there is no item in buildings that has undefined as the value for buildingID, it fails.
This is because the find method will return undefined when the condition couldn't be satisfied.
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
MDN Reference
In the computed property you are passing the whole building rather than the id, whereas in your method (in find) you are comparing the ids.
You need to pass the building id to your method.
Also in your data you are declaring building as an object, and then in your computed property, you are asserting whether building equals New which is a string.
I would advise the first thing you have to do is rethink your data types and the flow of your data between the template, computed properties, methods and data.
I'm using Vue Froala to do the image upload using the Froala config options which are basically an object added to the Vue data property. Something like this:
data() {
return {
config: {
imageUploadURL: '/api/image/store',
imageUploadParam: 'image',
imageUploadParams: {id: this.id} //this is the dynamic ID
}
}
}
When I upload an image and those config options are triggered, the ID that I'm trying to pass is always undefined. I guess it is because when I first load the page, the Vue data properties are initialized at the very beginning and the dynamic ID that I'm retrieving from a prop, is not yet available.
I tried assigning the data property imageUploadParams a computed property that would return the ID but was still undefined
If I use a watcher to assign the ID afterwards, I get null. Here is what I get in the POST code (php)
+request: Symfony\Component\HttpFoundation\ParameterBag {#45
#parameters: array:1 [
"id" => null
]
}
Any idea?
You could use a watch:
data() {
return {
config: {
imageUploadURL: '/api/image/store',
imageUploadParam: 'image',
imageUploadParams: { id: '' }
}
}
},
watch: {
id(value) {
this.config.imageUploadParams.id = value;
}
}
Now whenever id changes, config.imageUploadParams will be set.
Assigning a variable in JavaScript sets the value at the time of the assignment. Since your id is empty when it's assigned, there's no reference to use later to access future async properties. Assigning a computed won't help for the same reason.
Using watch for async, and computed for sync is a good general rule
Edit from chat
It seems <froala> isn't able to respond to your reactive data and is only using the initial config value. Put a v-if="id" on the component to prevent it from being created until the id is ready:
<froala v-if="id">
I have created an ETL to transfer data from sybase to a mongoDB environment. In one place I am matching against a legacy id in order to populate the correct mongo ObjectId in the model.
This works for my first few thousand records, until I run into a record that's missing a legacy id, in which case I get an error because the model is expecting an objectId. Doing a null check in my ETL file isn't sufficient to handle this. I need to also handle the fact that the model is expecting a valid objectId. How would I do this?
My relevant ETL code looks like this:
let agency = await Agency.findOne({ agencyId: record.agent_house }).exec();
Which I drop in like this:
agency: {
id: agency._id ? agency._id : null,
// Other prop,
// Other prop
}
And the data then gets ported to the model, which looks like this:
agency: {
id: { type: mongoose.Schema.Types.ObjectId, ref: 'agencies' },
// other prop,
// other prop
}
How can I handle a situation where there is no value, which will cause the objectId assignment to fail even with the null check in place in the ETL file?
Using the mongoose ObjectId type, you can do:
agency: {
id: agency._id ? agency._id : new mongoose.mongo.ObjectId(),
// Other prop,
// Other prop
}
This Meteor server code tries to copy the newly created property _id into a sub document but failed to do so.
How can it be done?
edit:
The code uses matb33:collection-hooks.
MyCollection.after.insert(function(userId, doc) {
if (doc.element === 'myString') {
doc.values[0]._id = doc._id;
}
});
Mutating the doc in the after hooks of matb33:collection-hooks will not cause additional queries to be run. You will need to explicitly update the document if you wish to do so.
However, in this particular case, if you really need the duplicate _id in the document, you could generate an _id and specify it when inserting the document.
You can probably use MyCollection._makeNewID() method, as this API has not changed for a few years and it is what the Mongo package uses internally.
const _id = MyCollection._makeNewID();
const doc = {
_id,
values: [
{
_id,
...
}, {
...
}
]
};
MyCollection.insert(doc);
I have read and read the docs on these two methods, but for the life of me cannot work out why you might use one over the other?
Could someone just give me a basic code situation where one would be application and the other wouldn't.
reset sets the collection with an array of models that you specify:
collection.reset( [ { name: "model1" }, { name: "model2" } ] );
fetch retrieves the collection data from the server, using the URL you've specified for the collection.
collection.fetch( { url: someUrl, success: function(collection) {
// collection has values from someUrl
} } );
Here's a Fiddle illustrating the difference.
We're assuming here that you've read the documentation, else it'l be a little confusing here.
If you look at documentation of fetch and reset, what it says is, suppose you have specified the url property of the collection - which might be pointing to some server code, and should return a json array of models, and you want the collection to be filled with the models being returned, you will use fetch.
For example you have the following json being returned from the server on the collection url:
[{
id : 1,
name : "a"
}, {
id : 2,
name : "b"
}, {
id : 3,
name : "c"
}]
Which will create 3 models in your collection after successful fetch. If you hunt for the code of collection fetch here you will see that fetch will get the response and internally will call either reset or add based on options specified.
So, coming back to discussion, reset assumes that we already have json of models, which we want to be stored in collection, we will pass it as a parameter to it. In your life, ever if you want to update the collection and you already have the models on client side, then you don't need to use fetch, reset will do your job.
Hence, if you want to the same json to be filled in the collection with the help of reset you can do something like this:
var _self = this;
$.getJSON("url", function(response) {
_self.reset(response); // assuming response returns the same json as above
});
Well, this is not a practice to be followed, for this scenario fetch is better, its just used for example.
Another example of reset is on the documentation page.
Hope it gives a little bit of idea and makes your life better :)
reset() is used for replacing collection with new array. For example:
#collection.reset(#full_collection.models)
would load #full_collections models, however
#collection.reset()
would return empty collection.
And fetch() function returns default collection of model