0

I've created an OData Endpoint with Node by using odata-server module by JayData in this way:

require("odata-server");

$data.Entity.extend("Service", {
  Id: {type: "id", key: true, computed: true, nullable: false},
  Name: {type: "string", nullable: false, maxLength: 50}
});

$data.EntityContext.extend("marketplace", {
  Services: {type: $data.EntitySet, elementType: Service}
});

$data.createODataServer(marketplace, "/marketplace", 8081, "localhost");
console.log("Marketplace OData Endpoint created... Listening at 8081.");

Then, still with Node, I've created an Express web application which receives some commands through GET request, connects to the OData Endpoint (still by using JayData) and receives some data from there, then sends back the result to the client (in the following code it just sends 200), in this way (by defining a route):

require("jaydata");
...
app.get("/addCompare/:id", function(req, res) {
  console.log("Comparison request for: " + req.params.id);
  $data.Entity.extend("Service", {
    Id: {type: "id", key: true, computed: true, nullable: false},
    Name: {type: "string", nullable: false, maxLength: 50}
  });
  $data.EntityContext.extend("marketplace", {
    Services: {type: $data.EntitySet, elementType: Service}
  });
  db = new marketplace("http://localhost:8081/marketplace");
  db.onReady(function() {
    var arr = db.Services.filter(function(s) {return s.Name.startsWith("Serv");}).toArray();
    console.dir(arr);
  });
  res.send(200);
});

The problem is that when I try this code (by using this GET request for example: http://www.localhost:8080/addCompare/NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5), I always get this error in the server and after that it crashes. Here's the error:

TypeError: Value '$data.Object' not convertable to '$data.ObjectID'
 { name: 'TypeError',
  message: 'Value \'$data.Object\' not convertable to \'$data.ObjectID\'',
  data:
   { __metadata:
      { type: 'Service',
        id: 'http://localhost:8081/marketplace/Services(\'NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5\')',
        uri: 'http://localhost:8081/marketplace/Services(\'NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5\')' },
     Id: 'NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5',
     Name: 'Service51' } }

Where am I wrong? Thanks...

Marco
  • 700
  • 1
  • 14
  • 26

1 Answers1

2

As the behavior was explained in OData - Strange index with MongoDB [Mongoose: Cast Error], the id - NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5 – should be base-64 decoded (for example 5343fd656b9c5c084b8f2a70 is a valid format). Although the declaration of JayData model is correct, it will be re-defined every single time when a request arrives to your server. You can improve the current implementation by moving your $data.Entity.extend and $data.EntityContext.extend blocks outside the app.get – after require("jaydata");.

Community
  • 1
  • 1
  • Thanks for your answer(s) Viktor. Can you please explain me how to correctly perform the decoding? I'm trying this: `db.Services.single(function(s){return s.Id == this.id;},{id: atob(req.params.id)},function(s){console.log(s);});` but I get the same error result... – Marco Apr 23 '14 at 13:02