Firebase Query using emberjs - firebase

I'm trying to sort a list of products from highest to lowest. The file json has been uploaded to firebase.
TypeError: Cannot read property 'query' of undefined
thats the error i'm receiving.
My model is called "product" and it has the price attribute
Here is the code:
sortHigh: ['product.price'],
actions:{
sortHigh(product) {
sortedProducts: Ember.computed.sort('product', 'sortHigh')
return this.store.query('product').sortHigh('price');
}
}
HTML {{action 'sortHigh'}}Highest Price

By default store is available only in route and controller. If you want to access store in any other place, then you need to inject to get it.
store: Ember.inject.service()

Related

How to manually set the Firestore document ID saving a new document calling Firestore API?

I am finding the following problem trying to save a new document into Firestore database by calling the related POST API and manually setting the document ID.
I am using Python but I suppose that the problem is not related to the language.
I try to explain what I have done and what is not working. My first attempt (that works but automatically set the document ID on Firestore) was:
First of all, I created this JSON document that will be the payload of my API:
# Convert the record to a dictionary
doc = {
'fields': {
'surname': {'stringValue':record[2]},
'firstName': {'stringValue':record[1]},
'socialSecurityCode': {'stringValue':codici_fiscali_list_as_string},
'city': {'stringValue':record[4]},
'personalPhone': {'stringValue':record[5]},
'personalPhone2': {'stringValue':record[6]},
'personalEmail': {'stringValue':emails_list_as_string},
'pazPres': {'stringValue':record[7]},
'pazNotes': {'stringValue':record[8]},
'pazMemo': {'stringValue':record[9]},
'isArchived': {'booleanValue':isArchived},
'isMigrated': {'booleanValue':True},
#'decomposition_keyword_search_list':{'arrayValue':{'values':decomposition_keyword_search_list}}
"decomposition_keyword_search_list":{
"arrayValue":{
"values":[
]
}
}
}
}
then I perform the API call by these lines:
api_endpoint = 'https://firestore.googleapis.com/v1/projects/MY-PROJECT-NAME/databases/(default)/documents/test/'
response = requests.post(api_endpoint, json=doc)
It works fine and it put the expected document into my test collection. But in this way, the ID was automatically generated by Firestore. For some reason, I have to use the content of a variable as ID (my ID must be the value of my record[0] that is a unique string)
So I tried to change the previous API endpoint in the following way:
api_endpoint = 'https://firestore.googleapis.com/v1/projects/MY-PROJECT-NAME/databases/(default)/documents/test/'+ record[0]
I expected that it creates a document using the record[0] as a document ID but it seems that I am wrong since I am obtaining the following error message:
Error saving document: {
"error": {
"code": 400,
"message": "Document parent name \"projects/MY-PROJECT-NAME/databases/(default)/documents/test\" lacks \"/\" at index 71.",
"status": "INVALID_ARGUMENT"
}
}
So, what is wrong? What am I missing? How can correctly manually set the ID of the document that I am creating calling the previous API?
Take a look at the documentation for creating documents. If you want to specify a document ID, it says you should pass that as a query parameter called documentId:
The client-assigned document ID to use for this document.
Optional. If not specified, an ID will be assigned by the service.

How to filter field value and send data into the firestore collection?

In the firestore collection named 'doctor' there are different fields including the field 'role'. I want to add the doctors into firestore with a role named doctor. How can I do this? Following is the code that successfully adds data into the database. If you can, tell me the way to add data with a specific field name. Thanks in advance.
service.ts
create_Newdoctor(Record){
return this.firestore.collection('doctors').add(Record);
}
component.ts
CreateRecord(docForm: NgForm){
let Record = {};
Record['fullName']=this.fullName;
Record['email']=this.email;
this.DoctorService.create_Newdoctor(Record).then(res=> {
this.fullName="";
this.email="";
console.log(res);
this.message = "Added";
}).catch(error=>{
console.log(error);
});
}
Notice that Record is a javascript object, and how you create a document in Cloud firestore, is by passing an object with all the filled attributes into the add method like what you did in service.ts, and how you pass in an attribute is via Record[ATTRIBUTE_NAME] = ATTRIBUTE_VALUE
Hence I believe what you need to just to add in the line Record[‘role’] = “doctors” into component.ts

Vue-Firebase: Updating children & writing to child

I started working with Firebase and Vue also with VueFire and i dont understand how to update child nodes at Firebase.
I opened a firebase project and connected to it and i can push data to it.
Firebase
I made a vue component
import db from '../FireBase'
let team = db.ref('Teams');//Reference to Teams at firebase
let miss = db.ref().child('Teams'); //Attempt to get to the children of Teams
export default {
name: "App",
firebase: {
Teams_loc: db.ref('Teams'),
Mission: this.Teams_loc.child('Teams'),
missionKey: db.ref().child('Teams').push("").key,
},
...
I manage to get the Teams from firebase and send data to it:
this.$firebaseRefs.Teams_loc.push({
"test": "tester"
});
Which works but when i try to update the children inside
this.miss.push({
"where": "am i"
})
I get the following error
Cannot read property 'child' of undefined
And when i try to update a child
this.$firebaseRefs.missionKey.update(arr[0]);//arr[0] is an object
I tried looking at quite a few places but nothing seems to do the trick.
Thanks,
When you do the following you are doing an error at the second line.
Teams_loc: db.ref('Teams'),
Mission: this.Teams_loc.child('Teams'),
There is no child of the Teams node that has a key with the value `Teams.
So if you want to update an item, you first have to get its key (e.g. -LEzOBT-mp.....) and do as follows, as explained in the doc:
updateItem: function (item) {
// create a copy of the item
const copy = {...item}
// remove the .key attribute
delete copy['.key']
//possibly update (or add) some values of (to) the item
this.$firebaseRefs.Teams_loc.child(item['.key']).set(copy)
}
Also (if I am not mistaking) doing db.ref() will generate an error because you have to pass a value to ref().
I suggest that you study a bit more the doc and the example: https://github.com/vuejs/vuefire and https://github.com/vuejs/vuefire/blob/master/examples/todo-app/index.html
Update following your comment. Details on how to "know the random generated key"
According to the documentation:
Each record in the bound array will contain a .key property which
specifies the key where the record is stored. So if you have data at
/Teams/-LEzOBT-mp...../, the record for that data will have a .key of
"-LEzOBT-mp.....".
Look at this part of the doc: https://github.com/vuejs/vuefire#array-bindings.
So with this you will get all the keys of the Teams object. You have now to choose the item you want to update.
You could also declare a query in your firebase object, like:
firebase: {
team21483: this.database
.ref('Teams')
.orderByChild('teamCode')
.equalTo('21483')
}
and you would get an array with only one team, the one with TeamCode = 21483.
The best approach, in this latest case, is to manually bind to this Firebase query with the $bindAsArray (or possibly the $bindAsObject) instance methods, using a variable that you pass to equalTo().

Exporting list view "as is" in sonata admin

In sonata it is possible to export list views to different file formats e.g. excel, pdf or csv ...
This export is based on the database query running in the background. It does not consider that maybe a template is manipulating the output in the list view. For example if a timestamp is saved in the database and this field is added via
$listMapper->add('testdate', 'date', ['format' => 'y-m-d'])
on the listview its correct displayed as "2017-10-01" but in the export there is something like "2489489289" in this column...
Another example: if i render a custom column representing a state based on different properties of the current object, the state column will never appear in the exported file.
Maybe a possible solution to me could be to override the admin controller and add a custom callback function to render the export file manual.
But here is one big problem:
I have to implement the whole logic for showing the columns in the expected format also in the exporting callback function.
Is there an elegant way to achieve this? What i actually want is an export of the current list view "as it is" and not based on the database query in the background ...
Thanks
You can add what fields you want to export or you can add functions and do your own logic for fields instead of field getters in getExportFields() method of your Admin class, read more here.
I have it like this in Admin:
public function getExportFields()
{
return array(
$this->trans('export.createdAt') => 'CreatedAtForExport',
$this->trans('export.OfferPage') => 'OfferPageNameForExport'
);
}
and in my Entity I have:
class Entity
{
public function getCreatedAtForExport()
{
return $this->createdAt->format('d.m.Y H:i');
}
public function getOfferPageNameForExport()
{
return $this->isOfferPage ? 'OfferPage' : 'CalcPage';
}
}

How to access and change a specific attribute in a collection?

I have a collection called "Products". I want to access and change an attribute called "screenShots" inside the collection.
This code didn't work with me
screenshotsURLS: function(sshots) {
check(sshots, [String]);
Products.update({},{$set:{screenShots:sshots}});
console.log(sshots);
}
when i console.log the sshots, i can see that the array exists, but the update function isn't working
how do i set the screenShots attribute inside the Product collection to whatever value passed in "screenshotsURLS" function?
For that you have to update the mongodb document.
THis is how you can update the doc in meteor.
collectionName.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
In your your case collectionName is Products and field is screenShots.
So for doing this. Your query will be
sshots
Products.update({},{$set:{screenShots:sshots}}) (Be careful this will update all of your doc)
For selecting doc update use the query like.
Products.update({name:'yourProductName'},{$set:{screenShots:sshots}})
For more about update a please check this link.

Resources