How to Get a Value from Firebase Using Ionic 4 - google-cloud-firestore

Sorry to ask. I'm just a noob here :(
Using Ionic 4 + Firebase, just for learning, say I have a button on html with a (click) function and want to be able to get a value!!! Banging my head on the wall!
example:
Firebase DB:
collection "Places"
doc:
id: 1
name: somename
want to click the button and by hard-coding the id, I want to alert(somename)
when I click I get [object Object], cannot get into the object.
code:
export class Tab4Page implements OnInit {
lugar: AngularFirestoreCollection<Places>;
lugares: Observable<Places[]>;
constructor(private _angFireStore: AngularFirestore) {
this.lugar = this._angFireStore.collection('Places');
this.lugares = this.lugar.valueChanges();
}
ngOnInit() { }
//this is the function
pruebaF1() {
alert(this.lugares); //What's next here???!!
}
}
sorry I can't get it.
do not know how to further dig into this.lugares to hardcode an id and just get a value: say, get nombre where id = 1, then nombre = somename
and if I say id = 2, then nombre = the name that's in the db for that doc.
not angular wise :(
Thanks in advance!!!! :)

I can see you are genuinely trying here. I see scenarios like this quite a lot and always wonder how people got this far in the first place. I'm curious, are you following a guide or just trying random bits of code?
The best thing to do is to follow some getting started tutorials which will walk you through each of the steps of the basics. Do what they want to teach you first, and then afterwards start building your own by editing bits until you find yourself writing the whole thing without problems.
This seems like an interesting guide, or the official angularfire2 docs.
Basically you are missing some concepts which would be best explained in a well written tutorial. What you get back with your code so far is an observable. That could be used in the front end with something like this:
<ul>
<li *ngFor="let item of lugares | async">
{{ item.name }} is {{ item.price }} (assuming these properties exist)
</li>
</ul>
But what you are actually asking is not this. You are writing code to access a collection, but to get a single one where id = 1 type query is a document, which would be more like:
this.placeRef = this.afs.collection(''Places'', ref => ref.where('id', '==', '1'));
But then that still gives you the missing understanding of what to do with it after. I think the best thing is to dig into the getting started documentation.
BTW the original tag you had, firebase realtime database is the old database and a very different technology, so if you are searching for information you need to make sure you are using the correct "firestore" search term.

Related

IONIC 1, Increment Child value after event

I am currently upgrading an old IONIC 1 app (adding new features). I have read a lot of threads but didn't find any answer to my question.
First of all, my Firebase Database structure looks as follows:
-products
--productId
--- imageurl
--- price
--- title
--- likes: 0
I would like to increment the value of child likes everytime a user triggers (for instance clicks a like button).
my service.js function looks as follows:
incrementLikeproduct: function(prodId){
return ref.child('products').child(prodId).child('likes').set( xxxx );
},
I have tried to retrieve the current value of the likes child object from firebase as a var and add +1; but didn't get anything to work properly. I basically do not know how to replace the xxxxx in my function to obtain the expected result.
My question is: How shoud I write the incrementLikeproduct function to increment the likes child value in firebase? I have read about asynchronous listener but can't figure out how to implement it inside my service.js.
I know that IONIC1 is outdated but I had already a solid piece of code to which I wanted to add new features without starting the whole app dev from scratch.
I am quite noob with JS and would be greateful if someone could give me a hint :-).
You should update your likes like below code:
return ref.child('products').child(prodId).update({
likes:xxx
})
thanks

Connecting to MongoDB console, and database template

I'm working through this tutorial:
https://medium.com/#matejhlavacka/6-in-6-challenge-week-1-yik-yak-web-clone-81938f3a033e
I'm unable to have the templates appear on my app, and I think I'm being held up on the two commands telling you to open a MongoDB console and type meteor mongo, and input this code:
db.yaks.insert({yak:"first yak ever"});
I've tried to download robomongo and sifted through many other resources, but still can't figure out why my page looks like below, rather than showing links:
http://wordofmouth.meteor.com/
Would appreciate any help.
Thanks all,
Eric
You probably don't have a template named "Home". The first argument after this.render is the name of the template.
Router.route('/', function () {
// 'Home' is the name of the template
this.render('Home', {
data: function () { return Items.findOne({_id: this.params._id}); }
});
});
You should have something like
<template name="Home">
</template>

CQ5.5: getting Style of a target page

I've been working on this for sometime now, and I keep running into a wall. I think I'm close, but I figured someone out here in the land of SO might have some deeper insight if not a better way of doing what I'm trying to do.
Basically lets look at this scenario. I have a logo w/ some text that can be set from a few different places. If we look at the setup here is what it looks like.
Hiearchy:
Homepage [has designPath]
- Child Microsite Page [has designPath]
- Logo Component
Logic Flow (in logo component):
if properties.get("logoText") {
use this
} else if currentStyle.get("logoTextFromStyle") {
use this
} else if parentStyle.get("logoTextFromGlobal") {
use this
} else {
be blank
}
My query is with how to get the "parentStyle" of this page. Looking at the docs here: http://dev.day.com/docs/en/cq/5-5/javadoc/com/day/cq/wcm/api/designer/Style.html
I've been able to come up with the fact that I can get a Style object from the "designer" object made available via defineObjects. This is defined with the other utility objects like "pageManager, resourceUtil, resource, currentPage, etc".
With that being said this doesn't seem to work.
//assuming we have getting homePage earlier and it is a valid cq Page resource
Resource homePageResource.slingRequest.getResourceResolver().getResource(homePage.getPath());
Style homePageStyle = designer.getStyle(homePageResource);
at this point homePageStyle is null. To do some more testing I i tried passing currentPage.getPath() instead of homePage.getPath(). I assumed this would give me the currentPage resource and would in end yield the currentStyle object. This also resulted in a null Style object. From this I think I can safely conclude I'm passing the incorrect resource type.
I attempted to load the the cq:designPath into the resource hoping to get a Designer resourceType but to no avail.
I am curious if anyone has run into this problem before. I apologize if I've gone into too much detail, but I wanted to lay out the "why" to my question as well, just in case there was a better way overall of accomplishing this.
I've figured out how to return the style. Here is the rundown of what I did.
//get your page object
Page targetPage = pageManager.getPage("/path/to/target");
//get the Design object of the target page
Design homePageDesign = designer.getDesign(homePage);
//extract the style from the design using the design path
Style homePageStyle = homePageDesign.getStyle(homePageDesign.getPath());
it's very interesting the definition of "getStyle" is a little different from the designer.getStyle vs a Design.getStyle. designer.getStyle asks for a resource whereas Design.getStyle will take the path to a Design "cell" and return the appropriate Style.
I did some testing and it looks like it does work with inherited Styles/Designs. So if my cq:designPath is set at level 1 and I look up a page on at level 2 they will return the Design/Style at the cq:designPath set at level 1.
I hope this helps someone else down the way.
I tried this approach but was not getting the Styles in the Style object.
When we do this:
Design homePageDesign = designer.getDesign(homePage);
In this Design object we get the path till the project node i.e etc/design/myproject
After this if we try to extract the Style from the design path we do not get it.
However I implemented it in a different way.
In the design object, we also get the complete JSON of designs for(etc/design/myproject).
Get the sling:resourceType of the target page and get the value after last index of "/".
Check if this JSON contains the last value. If it contains, you can get your styles, i.e. image, etc.

How to remove an Embed Document from an Embed Document?

I'm currently working on a project with Symfony 2 and MongoDB and I'm facing a problem while removing an Embed Document using is ID.
I'm working with Activities. An Activity embed a list of Comment and list of Vote.
A Comment can embed a list of Vote too.
To remove a Comment or a Vote from an Activity I do stuff like that :
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$activity = $dm->getRepository('SocialNetworkActivityBundle:Activity')
->findOneBy(array("votes.id" => $voteId));
$votes = $activity->getVotes();
$targetVote = null;
foreach ($votes as $vote)
if($vote->getId() == $voteId) {
$targetVote = $vote;
break;
}
$activity->removeVote($vote);
$dm->flush();
First of all, I know that the method I use isn't the best so I'm looking for an alternative.
Second of all, I can't use that method to remove a Vote embed in a Comment embed in an Activity, so I'm looking for a way to do that.
I don't find any solution in Doctrine documentation or on Stack (or other forum).
If you have some clues or advises for me I'll be really glad to read about :D

How do I build an Index in Nhibernate.Search (Lucene.Net)?

I'm trying to add a search field to my web site (ASP.NET MVC 2) and was told it'd be a good idea to use Nhibernate.Search, seeing that I was already using Nhibernate in the rest of the project.
Anyway, I followed a coulpe tutorials, namely this one, and some questions and answeres on this site, but in the end, it does not build an index, and searches come empty.
I know this question might be a bit vague, but it seems strange that nothing works even after I've done everything I was told.
Well, almost everything. At some point, in one of the tutorials, it tells me to type:
using (IFullTextSession s = Search.CreateFullTextSession(sf.OpenSession(new SearchInterceptor()))) {
QueryParser qp = new QueryParser("id", new StopAnalyzer());
IQuery NHQuery = s.CreateFullTextQuery(qp.Parse("Summary:series"), typeof(Book));
IList result = NHQuery.List();
Debug.Assert(result.Count == 2);
}
wich does not work because SearchInterceptor does not exist anywhere...
Am I missing something here?
Is there a way to better write the search queries?
In which part of my application does it build the index?
Thanks in advance.
I've tried something like:
public bool LuceneIndexAllVideos()
{
var s = NHibernate.Search.Search.CreateFullTextSession(Session);
foreach (Video video in Videos)
{
s.Index(video);
}
return true;
}
But is slow, but it seems to work nice...
See:
https://stackoverflow.com/questions/6989125/lucene-net-nhibernate-updating-lucene-index-from-existing-data