[dancer-users] Question on MongoDB and Dancer
Gavin Colborne
gavin at littleforest.co.uk
Thu Oct 19 07:22:46 BST 2017
Thanks Richard and everyone,
I added the ".item" so my view shows: [% entries.$id.item('time') %] I
still don't see any results :(
I now see in my terminal the following: Argument "client" isn't numeric in
numeric comparison (<=>)
Which from what I can tell from Gabor's great site
https://perlmaven.com/argument-isnt-numeric-in-numeric is some numeric
operation problem.
Do you have any examples of controllers passing Mongo data to views so I
can compare?
My code so far - perhaps the controller is the issue?
sub connect_mongo {
my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error
connecting to Mongo";
my $db = $client->get_database( 'lfi-perl' );
return($db);
}
get '/mongo' => sub {
my $db = connect_mongo();
my $docs = $db->get_collection( 'Test-Collection' );
my $all_docs;
template 'show_mongo.tt', {
'entries' => $all_docs = $docs->find(),
};
};
Appreciate your help,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014
Mobile: 0788 400 4339
Skype: gavincolborne
Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
On 18 October 2017 at 10:13, Richard Jones <ra.jones at dpw.clara.co.uk> wrote:
> I've been caught by that before. Try [% entries.$id.item('_id') %]. For
> some reason (would interested to learn what), TT doesn't seem to be able to
> cope with leading underscores in data structures. You can prove entries
> contains data using:
> [% USE Dumper(Indent=1) %]
> [% FOREACH id IN entries %]
> <pre>[% Dumper.dump(id) %]</pre>
> [% END %]
>
> or just [% Dumper.dump(entries) %]
>
> On 18/10/2017 06:28, Gavin Colborne wrote:
>
> Hi Dancers,
>
> I am pretty new to Dancer and really like the light nature of the
> framework.
>
> I am trying to create a route which will show data from a MongoDB
> collection and struggling with the syntax.
>
> I have the following in my app:
>
> sub connect_mongo {
> my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error
> connecting to Mongo";
> my $db = $client->get_database( 'lfi-perl' );
> return($db);
> }
>
> get '/mongo' => sub {
> my $db = connect_mongo();
> my $docs = $db->get_collection( 'Test-Collection' );
> my $all_docs;
> template 'show_mongo.tt', {
> 'entries' => $all_docs = $docs->find(),
> };
> };
>
> Then in my view the following:
>
> [% FOREACH id IN entries.keys.nsort %]
>
> [% entries.$id._id %]
>
>
>
> I am not able to see any data in my view but am also not seeing any errors.
>
> Any examples of getting data from Mongo in Dancer would be really helpful.
>
> Thank you in advance,
>
> Gavin
>
>
>
>
>
>
>
> Best Regards,
>
> Gavin Colborne
>
> Managing Director,
>
> Tele: 0207 193 2014
> Mobile: 0788 400 4339
> Skype: gavincolborne
> Visit: www.littleforest.co.uk
>
> [image: Little Forest LFi] <http://www.littleforest.co.uk/>
>
>
> _______________________________________________
> dancer-users mailing listdancer-users at dancer.pmhttp://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
> --
> Richard Jones
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20171019/f2c66e5f/attachment.html>
More information about the dancer-users
mailing list