Thread overview
VibeD - undefinded identifier
Jul 23, 2017
holo
Jul 23, 2017
Seb
Jul 23, 2017
holo
Jul 23, 2017
Mike Parker
Jul 23, 2017
holo
Jul 23, 2017
Seb
Jul 23, 2017
holo
July 23, 2017
Hello

Im trying to create REST api for my simple web page. My code is looking like below:

module service.frontpage;

import vibe.d;

@path("/api")
interface IFrontPageAPI
{
  Json getHome();
}

class FrontPageAPI : IFrontPageAPI
{


  this(auto tmp)
  {
    auto collect = tmp;
  }

  Json getHome()
  {
    logInfo("Getting HomePage from DB");
    Bson query = Bson(["_id" : Bson("homepage")]);
    auto result = collect.find(query);


    logInfo("Iterating results...");
    foreach (i, doc; result.byPair)
    logInfo("Item %d: %s", i, doc.toJson().toString());

    return result.toJson();
  }
}

But when i try to compile it im getting:

source/service/frontpage.d(15,3): Error: undefined identifier 'tmp', did you mean alias 'cmp'?

Did i miss something?
July 23, 2017
On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote:
> Hello
>
> Im trying to create REST api for my simple web page. My code is looking like below:
>
> module service.frontpage;
>
> import vibe.d;
>
> @path("/api")
> interface IFrontPageAPI
> {
>   Json getHome();
> }
>
> class FrontPageAPI : IFrontPageAPI
> {
>
>
>   this(auto tmp)
>   {
>     auto collect = tmp;
>   }
>
>   Json getHome()
>   {
>     logInfo("Getting HomePage from DB");
>     Bson query = Bson(["_id" : Bson("homepage")]);
>     auto result = collect.find(query);
>
>
>     logInfo("Iterating results...");
>     foreach (i, doc; result.byPair)
>     logInfo("Item %d: %s", i, doc.toJson().toString());
>
>     return result.toJson();
>   }
> }
>
> But when i try to compile it im getting:
>
> source/service/frontpage.d(15,3): Error: undefined identifier 'tmp', did you mean alias 'cmp'?
>
> Did i miss something?

You need to
- use Class variables
- use types instead auto (here Mongo collection)
- return a proper Json

Have a look at: https://is.gd/7AMsKs

And play with it - no output means no complication error.
July 23, 2017
On Sunday, 23 July 2017 at 15:43:31 UTC, Seb wrote:
> On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote:
>> Hello
>>
>> Im trying to create REST api for my simple web page. My code is looking like below:
>>
>> module service.frontpage;
>>
>> import vibe.d;
>>
>> @path("/api")
>> interface IFrontPageAPI
>> {
>>   Json getHome();
>> }
>>
>> class FrontPageAPI : IFrontPageAPI
>> {
>>
>>
>>   this(auto tmp)
>>   {
>>     auto collect = tmp;
>>   }
>>
>>   Json getHome()
>>   {
>>     logInfo("Getting HomePage from DB");
>>     Bson query = Bson(["_id" : Bson("homepage")]);
>>     auto result = collect.find(query);
>>
>>
>>     logInfo("Iterating results...");
>>     foreach (i, doc; result.byPair)
>>     logInfo("Item %d: %s", i, doc.toJson().toString());
>>
>>     return result.toJson();
>>   }
>> }
>>
>> But when i try to compile it im getting:
>>
>> source/service/frontpage.d(15,3): Error: undefined identifier 'tmp', did you mean alias 'cmp'?
>>
>> Did i miss something?
>
> You need to
> - use Class variables
> - use types instead auto (here Mongo collection)
> - return a proper Json
>
> Have a look at: https://is.gd/7AMsKs
>
> And play with it - no output means no complication error.

Thank you it solves my problem, but now i'm getting:

source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are:
/usr/include/dlang/dmd/std/array.d(419,6):        std.array.byPair(Key, Value)(Value[Key] aa)
source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function

What is strange it is just copy/paste from example: https://github.com/rejectedsoftware/vibe.d/blob/master/examples/mongodb/source/app.d


July 23, 2017
On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote:

>   this(auto tmp)

You need to specify a type here instead of using auto.


July 23, 2017
On Sunday, 23 July 2017 at 16:27:40 UTC, Mike Parker wrote:
> On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote:
>
>>   this(auto tmp)
>
> You need to specify a type here instead of using auto.

Thanks for reply i fix it. But right now im getting:

../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead.
source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are:
/usr/include/dlang/dmd/std/array.d(419,6):        std.array.byPair(Key, Value)(Value[Key] aa)
source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function

July 23, 2017
On Sunday, 23 July 2017 at 16:37:19 UTC, holo wrote:
> On Sunday, 23 July 2017 at 16:27:40 UTC, Mike Parker wrote:
>> On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote:
>>
>>>   this(auto tmp)
>>
>> You need to specify a type here instead of using auto.
>
> Thanks for reply i fix it. But right now im getting:
>
> ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead.
> source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are:
> /usr/include/dlang/dmd/std/array.d(419,6):        std.array.byPair(Key, Value)(Value[Key] aa)

What are you trying to do with byPair?
It's only defined for the MongoCursor: http://vibed.org/api/vibe.db.mongo.cursor/MongoCursor.byPair

That std.array.byPair shows up means that you are using it for something else?

> source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function

I guess you forgot a return here?
Maybe it's easier to post your code again?
July 23, 2017
On Sunday, 23 July 2017 at 17:16:44 UTC, Seb wrote:
> On Sunday, 23 July 2017 at 16:37:19 UTC, holo wrote:
>> On Sunday, 23 July 2017 at 16:27:40 UTC, Mike Parker wrote:
>>> On Sunday, 23 July 2017 at 15:23:25 UTC, holo wrote:
>>>
>>>>   this(auto tmp)
>>>
>>> You need to specify a type here instead of using auto.
>>
>> Thanks for reply i fix it. But right now im getting:
>>
>> ../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d(286,33): Deprecation: alias diet.traits.FilterCallback is deprecated - Use SafeFilterCallback instead.
>> source/service/frontpage.d(28,30): Error: template std.array.byPair cannot deduce function from argument types !()(MongoCursor!(Bson, Bson, typeof(null))), candidates are:
>> /usr/include/dlang/dmd/std/array.d(419,6):        std.array.byPair(Key, Value)(Value[Key] aa)
>
> What are you trying to do with byPair?
> It's only defined for the MongoCursor: http://vibed.org/api/vibe.db.mongo.cursor/MongoCursor.byPair
>
> That std.array.byPair shows up means that you are using it for something else?
>
>> source/service/frontpage.d(21,10): Error: function frontpage.FrontPageAPI.getHome no return exp; or assert(0); at end of function
>
> I guess you forgot a return here?
> Maybe it's easier to post your code again?

I took this code from example: https://github.com/rejectedsoftware/vibe.d/blob/master/examples/mongodb/source/app.d

And it is looking right now like below:

import vibe.d;
import std.algorithm, std.array;

@path("/api")
interface IFrontPageAPI
{
    Json getHome();
}

class FrontPageAPI : IFrontPageAPI
{
  this(MongoCollection tmp)
  {
      collect = tmp;
  }

  private:
    MongoCollection collect;

  public:
    Json getHome()
    {
      logInfo("Getting HomePage from DB");
      Bson query = Bson(["_id" : Bson("homepage")]);
      auto result = collect.find(query);

      logInfo("Iterating results...");
      foreach (i, doc; result.byPair)
        logInfo("Item %d: %s", i, doc.toJson().toString());

      logInfo("Sending Json");
      return Json(result.map!(a => a.toJson).array);
    }
}

When i remove this piece of code:

      logInfo("Iterating results...");
      foreach (i, doc; result.byPair)
        logInfo("Item %d: %s", i, doc.toJson().toString());

everything is working as expecting.

One more question, what it does mean:

      return Json(result.map!(a => a.toJson).array);

Why we cant just use something like result.toJson?


When i