pofider / node-simple-odata-server

Simple OData server for node.js
MIT License
97 stars 61 forks source link

Example throws exception #9

Closed htammen closed 9 years ago

htammen commented 9 years ago

Hi, when I try to run your example from the examples folder I get the following error as soon as I try to read the users entityset.

/Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/prune.js:15
        var propDef = type[prop];
                          ^
TypeError: Cannot read property '_id' of undefined
    at prune (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/prune.js:15:27)
    at prune (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/prune.js:5:13)
    at module.exports (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/prune.js:54:9)
    at ODataServer.pruneResults (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/odataServer.js:226:5)
    at /Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/query.js:74:13
    at /Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/odataServer.js:139:13
    at /Users/helmut/projekte/node/simple-odata-server-test/node_modules/simple-odata-server/lib/nedbAdapter.js:56:24
    at queue.async.queue.callback (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/nedb/lib/executor.js:30:17)
    at Cursor.execFn (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/nedb/lib/datastore.js:424:12)
    at Cursor._exec (/Users/helmut/projekte/node/simple-odata-server-test/node_modules/nedb/lib/cursor.js:172:17)

Unfortunately I'm not able to debug with node-debug (cannot set a breakpoint, don't know why, I'm quite new to node). Any ideas whats wrong with the example / the odata-server?

Here's my code:

var http = require('http');
var Datastore = require('nedb');
var db = new Datastore( { inMemoryOnly: true });
var ODataServer = require("simple-odata-server");

var model = {
    namespace: "myns",
    entityTypes: {
        "myns.UserType": {
            "_id": {"type": "Edm.String", key: true},
            "test": {"type": "Edm.String"},            
        }
    },   
    entitySets: {
        "users": {
            entityType: "myns.UserType"
        }
    }
};

var odataServer = ODataServer("http://localhost:1337")
    .model(model)
    .onNeDB(function(es, cb) { cb(null, db)});

http.createServer(odataServer.handle.bind(odataServer)).listen(1337);

db.insert({"_id": "1", "test": "a"});
db.insert({"_id": "2", "test": "b"});
db.insert({"_id": "3", "test": "c"});
db.insert({"_id": "4", "test": "d"});
db.insert({"_id": "5", "test": "e"});

Best regards Helmut

pofider commented 9 years ago

Hi Helmut,

entity type should not include namespace in the model

your fixed code should look like

var http = require('http');
var Datastore = require('nedb');
var db = new Datastore( { inMemoryOnly: true });
var ODataServer = require("simple-odata-server");

var model = {
    namespace: "myns",
    entityTypes: {
        "UserType": {
            "_id": {"type": "Edm.String", key: true},
            "test": {"type": "Edm.String"},            
        }
    },   
    entitySets: {
        "users": {
            entityType: "myns.UserType"
        }
    }
};

var odataServer = ODataServer("http://localhost:1337")
    .model(model)
    .onNeDB(function(es, cb) { cb(null, db)});

http.createServer(odataServer.handle.bind(odataServer)).listen(1337);

db.insert({"_id": "1", "test": "a"});
db.insert({"_id": "2", "test": "b"});
db.insert({"_id": "3", "test": "c"});
db.insert({"_id": "4", "test": "d"});
db.insert({"_id": "5", "test": "e"});

Jan

htammen commented 9 years ago

Thank you very much Jan for this quick response, nearly real time :-). It's working :+1: Maybe you should update your example. I've copied the code from it.

Thanks Helmut

pofider commented 9 years ago

Oh, thanks. Just fixed it in the readme.