javascriptnode.jsmongodbdhtmlx-scheduler

Typer Error: Cannot read property 'length' of undefined


I’m creating a node.js app and implementing the scheduler app. I’m following this walkthrough: https://dhtmlx.com/blog/using-dhtmlxscheduler-with-node-js/

When I navigate to the /data page, it says cannot read property 'length' of undefined. I went to the github page and downloaded app.js word for word.

Here is app.js portion throwing error:

app.get('/data', function(req, res){
db.event.find().toArray(function(err, data){
    //set id property for all records
    for (var i = 0; i < data.length; i++)
        data[i].id = data[i]._id;

    //output response
    res.send(data);
});
});

Solution

  • This example works fine. I guess, in your case, mongoskin fails to connect to mongodb for some reasons. If I change some parts in this path mongodb://localhost/testdb (for testing purpose, put some random string) , mongodb will complain about problem with url, and I'll get an error TypeError: Cannot read property 'length' of undefined from node js, because there will be no data fetched from database.