RejectedSoftware Forums

Sign up

Mongo client and Diet Templates

I am trying to retrieve records from a MongoDB database and display on a page using Diet templates.

I currently have the following template that causes a crash when I access it:

!!! 5
html
 head
  title Listing from Mongo DB
 body
  h Listing of images in my Mongo database.
  - import vibe.d;
  - MongoClient mongo_client;
  - auto image_lst = mongo_client.getCollection("mydb.images");
  - foreach(i; 0..4)
   - auto num = i;
   p 
    b= num
  p Thats all

Eventually my foreach() loop will print out the objects from the mydb.images collection, but I haven't gotten that far yet, because this template causes the server to crash with the error
Error: Program exited with code -11. If the line - auto image_lst = mongo_client.getCollection("mydb.images"); is removed then this works and prints out 0 to 3. I've tried the same Mongo code in my regular application and it works fine.

The first few stack frames from my backtrace:

#0 vibe.templ.diet.__T22compileDietFileCompatVVAyaa9_696d616765732e6474TC4vibe4http6server17HTTPServerRequestVAyaa3_726571Z.compileDietFileCompatV() (_arguments=..., _argptr=0x7ffff7ff82b0, 
    stream__=0x9cba58) at images.dt:7
#1  0x00000000005e3dff in vibe.http.server.HTTPServerResponse.__T12renderCompatVAyaa9_696d616765732e6474TC4vibe4http6server17HTTPServerRequestVAyaa3_726571Z.renderCompat() (this=0x9ab640, 
    _arguments_typeinfo=0x90d400 <TypeInfo_B37C4vibe4http6server17HTTPServerRequest.__init()>) at ../../.dub/packages/vibe-d-master/source/vibe/http/server.d:868
#2  0x0000000000566dc9 in app.listImages() (res=0x9ab640, req=0x99b620) at source/app.d:61
#3  0x000000000058a858 in std.functional.__T13DelegateFakerTPFC4vibe4http6server17HTTPServerRequestC4vibe4http6server18HTTPServerResponseZvZ.DelegateFaker.doIt() (
    this=0x566da0 <app.listImages()>, a1=0x9ab640, a0=0x99b620) at /usr/include/dmd/phobos/std/functional.d:705
#4  0x00000000005e5609 in vibe.http.router.URLRouter.handleRequest() (this=0x7ffff7ecaa00, res=0x9ab640, req=0x99b620) at ../../.dub/packages/vibe-d-master/source/vibe/http/router.d:159

For the sake of completeness the code that actually tries to generate the template is as follows (but the error seems to be with the template itself, as this works fine before adding the offending line to images.dt):

void listImages(HTTPServerRequest req, HTTPServerResponse res)
{
  res.renderCompat!("images.dt", HTTPServerRequest, "req")(req);
}

Re: Mongo client and Diet Templates

On Tue, 10 Sep 2013 17:50:57 GMT, Craig Dillabaugh wrote:

  • MongoClient mongo_client;
  • auto imagelst = mongoclient.getCollection("mydb.images");

You need to get your MongoClient object from the connectMongoDB() function like this:

MongoClient mongo_client = connectMongoDB(url);

The url should be something like: mongodb://localhost/mydb

Re: Mongo client and Diet Templates

On Wed, 11 Sep 2013 02:26:25 GMT, David Eagen wrote:

On Tue, 10 Sep 2013 17:50:57 GMT, Craig Dillabaugh wrote:

  • MongoClient mongo_client;
  • auto imagelst = mongoclient.getCollection("mydb.images");

You need to get your MongoClient object from the connectMongoDB() function like this:

MongoClient mongo_client = connectMongoDB(url);

The url should be something like: mongodb://localhost/mydb

This doesn't seem to work either. It hangs my server. To test things out I have the function:

void listImages(HTTPServerRequest req, HTTPServerResponse res)
{
  logInfo("Entering list images.");
  g_mongo_client = connectMongoDB("mongodb://localhost");

  auto images = g_mongo_client.getCollection("mydb.images");
  foreach( img; images.find() ) {
    logInfo("Image: %s", img.toJson() );
   }

  res.renderCompat!("images.dt", HTTPServerRequest, "req")(req);
}

If I comment out the connectMongoDB() line the server will list all the records in "mydb.images", but it still hangs/crashes when trying to generate the images.dt template. If I add a connectMongoDB line to my template then it hangs, if I use the same code as I have in my listImages(...) function, without connectMongoDB(), then it crashes as described in the original post.

*Entering list images" always gets logged, so I am assuming that it is connectMongoDB() that causes it to hang.

Re: Mongo client and Diet Templates

On Wed, 11 Sep 2013 16:49:14 GMT, Craig Dillabaugh wrote:

On Wed, 11 Sep 2013 02:26:25 GMT, David Eagen wrote:

On Tue, 10 Sep 2013 17:50:57 GMT, Craig Dillabaugh wrote:

  • MongoClient mongo_client;
  • auto imagelst = mongoclient.getCollection("mydb.images");

You need to get your MongoClient object from the connectMongoDB() function like this:

MongoClient mongo_client = connectMongoDB(url);

The url should be something like: mongodb://localhost/mydb

This doesn't seem to work either. It hangs my server. To test things out I have the function:

void listImages(HTTPServerRequest req, HTTPServerResponse res)
{
  logInfo("Entering list images.");
  g_mongo_client = connectMongoDB("mongodb://localhost");

  auto images = g_mongo_client.getCollection("mydb.images");
  foreach( img; images.find() ) {
    logInfo("Image: %s", img.toJson() );
   }

  res.renderCompat!("images.dt", HTTPServerRequest, "req")(req);
}

If I comment out the connectMongoDB() line the server will list all the records in "mydb.images", but it still hangs/crashes when trying to generate the images.dt template. If I add a connectMongoDB line to my template then it hangs, if I use the same code as I have in my listImages(...) function, without connectMongoDB(), then it crashes as described in the original post.

*Entering list images" always gets logged, so I am assuming that it is connectMongoDB() that causes it to hang.

I wouldn't really explain why it hangs, but it may be that you hit https://github.com/rejectedsoftware/vibe.d/issues/289 and need to replace "localhost" with "127.0.0.1". Without a connectMongoDB call it would be a mystery, though, if it still works. g_mongo_client would contain a null pointer after all.

If you are on Windows, you can also add "subConfigurations": {"vibe-d": "win32"} to your package.json to use the win32 driver, which doesn't have the "localhost" issue.

Re: Mongo client and Diet Templates

On Wed, 11 Sep 2013 17:10:48 GMT, Sönke Ludwig wrote:

On Wed, 11 Sep 2013 16:49:14 GMT, Craig Dillabaugh wrote:

On Wed, 11 Sep 2013 02:26:25 GMT, David Eagen wrote:

On Tue, 10 Sep 2013 17:50:57 GMT, Craig Dillabaugh wrote:

  • MongoClient mongo_client;
  • auto imagelst = mongoclient.getCollection("mydb.images");

You need to get your MongoClient object from the connectMongoDB() function like this:

MongoClient mongo_client = connectMongoDB(url);

The url should be something like: mongodb://localhost/mydb

This doesn't seem to work either. It hangs my server. To test things out I have the function:

void listImages(HTTPServerRequest req, HTTPServerResponse res)
{
  logInfo("Entering list images.");
  g_mongo_client = connectMongoDB("mongodb://localhost");

  auto images = g_mongo_client.getCollection("mydb.images");
  foreach( img; images.find() ) {
    logInfo("Image: %s", img.toJson() );
   }

  res.renderCompat!("images.dt", HTTPServerRequest, "req")(req);
}

If I comment out the connectMongoDB() line the server will list all the records in "mydb.images", but it still hangs/crashes when trying to generate the images.dt template. If I add a connectMongoDB line to my template then it hangs, if I use the same code as I have in my listImages(...) function, without connectMongoDB(), then it crashes as described in the original post.

*Entering list images" always gets logged, so I am assuming that it is connectMongoDB() that causes it to hang.

I wouldn't really explain why it hangs, but it may be that you hit https://github.com/rejectedsoftware/vibe.d/issues/289 and need to replace "localhost" with "127.0.0.1". Without a connectMongoDB call it would be a mystery, though, if it still works. g_mongo_client would contain a null pointer after all.

If you are on Windows, you can also add "subConfigurations": {"vibe-d": "win32"} to your package.json to use the win32 driver, which doesn't have the "localhost" issue.

It appears to the localhost issue (I am on Linux). The following now works in my images.dt template.

  - MongoClient mongoclient = connectMongoDB("127.0.0.1");
  - auto image_lst = mongoclient.getCollection("cast-tiles.images");

And the reason my code was working in my app.d is that I made a call elsewhere to connectMongoDB("127.0.0.1") on my global g_mongo_client variable (hangs head). Thus using g_mongo_client.getCollection() worked fine because g_mongo_client was already initialized, but the call to connectMongoDB("localhost") hung likely due to the bug you mentioned.

Re: Mongo client and Diet Templates

On Wed, 11 Sep 2013 17:52:54 GMT, Craig Dillabaugh wrote:

It appears to the localhost issue (I am on Linux). The following now works in my images.dt template.

  - MongoClient mongoclient = connectMongoDB("127.0.0.1");
  - auto image_lst = mongoclient.getCollection("cast-tiles.images");

And the reason my code was working in my app.d is that I made a call elsewhere to connectMongoDB("127.0.0.1") on my global g_mongo_client variable (hangs head). Thus using g_mongo_client.getCollection() worked fine because g_mongo_client was already initialized, but the call to connectMongoDB("localhost") hung likely due to the bug you mentioned.

Good to know. I've added a quick hack to ease the situation a bit until a proper host file parser is written - it happens too often that someone stumbles over this.

Re: Mongo client and Diet Templates

On Wed, 11 Sep 2013 18:46:36 GMT, Sönke Ludwig wrote:

On Wed, 11 Sep 2013 17:52:54 GMT, Craig Dillabaugh wrote:

It appears to the localhost issue (I am on Linux). The following now works in my images.dt template.

  - MongoClient mongoclient = connectMongoDB("127.0.0.1");
  - auto image_lst = mongoclient.getCollection("cast-tiles.images");

And the reason my code was working in my app.d is that I made a call elsewhere to connectMongoDB("127.0.0.1") on my global g_mongo_client variable (hangs head). Thus using g_mongo_client.getCollection() worked fine because g_mongo_client was already initialized, but the call to connectMongoDB("localhost") hung likely due to the bug you mentioned.

Good to know. I've added a quick hack to ease the situation a bit until a proper host file parser is written - it happens too often that someone stumbles over this.

Thanks!