JayData - OData查询获取" $ data.Object不能转换为$ data.ObjectID"

时间:2014-04-18 10:05:43

标签: node.js express odata jaydata

我通过JayData使用odata-server模块以这种方式创建了一个带节点的OData端点:

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.");

然后,仍然使用Node,我创建了一个Express Web应用程序,它通过GET请求接收一些命令,连接到OData端点(仍然使用JayData)并从那里接收一些数据,然后将结果发送回客户端(在下面的代码中它只发送200),以这种方式(通过定义路由):

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);
});

问题在于,当我尝试此代码时(通过使用此GET请求,例如:http://www.localhost:8080/addCompare/NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5),我总是在服务器中收到此错误,然后崩溃。这是错误:

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' } }

我哪里错了?感谢...

1 个答案:

答案 0 :(得分:2)

由于行为在OData - Strange index with MongoDB [Mongoose: Cast Error]中解释,id - NTM0M2ZkNjU2YjljNWMwODRiOGYyYTU5 - 应该是base-64解码的(例如5343fd656b9c5c084b8f2a70是有效格式)。 虽然JayData模型的声明是正确的,但是当请求到达您的服务器时,它将每次重新定义。您可以在$data.Entity.extend之后将$data.EntityContext.extendapp.get块移到require("jaydata");之外,从而改进当前的实施。