Node.Js x Mongodb - connection - cannot load the simple document

17 views
Skip to first unread message

Tarcísio Corte

unread,
Aug 5, 2016, 6:46:58 AM8/5/16
to mongodb-dev
Hi guys,

I'm trying to use Mongodb and Node.Js. I'm an experient .net developer but with few knowloge of Mongodb (I know I should use Mongodb frequently)

I've insert some documents

db.message.insert({message: 'Hello world'})


I got the follow error using a simple connection:

multivision db opened
c:\Dev\node-mean\server.js:40
  mongoMessage = messageDoc.message;
                           ^
TypeError: Cannot read property 'message' of null
    at c:\Dev\node-mean\server.js:40:28
    at Query.<anonymous> (c:\Dev\node-mean\node_modules\mongoose\lib\query.js:2233:28)
    at c:\Dev\node-mean\node_modules\kareem\index.js:259:21
    at c:\Dev\node-mean\node_modules\kareem\index.js:127:16
    at process._tickCallback (node.js:355:11)

The Node.Js that execute that is:

mongoose.connect('mongodb://localhost:27017/multivision');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error...'));
db.once('open', function callback() {
  console.log('multivision db opened');
});
var messageSchema = mongoose.Schema({message: String});

var Message = mongoose.model('message', messageSchema);
var mongoMessage;

Message.findOne().exec(function(err, messageDoc) {
  mongoMessage = messageDoc.message;
});

app.get('/partials/:partialPath', function(req, res) {
    res.render('partials/' + req.params.partialPath);
});

app.get('*', function(req, res) {
  res.render('index', {
    mongoMessage: mongoMessage
  });
});

var port = 3030;
app.listen(port);
console.log('Listening on port ' + port + '...');



Do I need to use "query"? What is? is it new in Mongodb?

Matt Kangas

unread,
Aug 7, 2016, 11:55:46 PM8/7/16
to mongo...@googlegroups.com
Hi Tarcisio,

FYI - this group is for developers who wish to help develop MongoDB (the database itself).

Since your question is primarily about how to use the Mongoose ORM, a better place to ask would be the mongoose-orm discussion group. You can find more resources on our Node.js ecosystem page.

I notice that the code snippet you posted follows the Mongoose Getting Started page, but you don’t have any code inside the ‘open’ callback. In Node.js, all I/O is asynchronous. Perhaps you overlooked this line in the tutorial?

> Once our connection opens, our callback will be called. For brevity, let’s assume that all following code is within this callback.

Since you are an experienced .NET developer, perhaps you may find it easier to get started with MongoDB using our C# and .NET driver?

Cheers,
Matt


--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev+unsubscribe@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
For more options, visit https://groups.google.com/d/optout.

Tarcísio Corte

unread,
Aug 8, 2016, 6:39:04 AM8/8/16
to mongodb-dev
Hi Matt,

Tks for help me.

I've checked the code out and I fixed it. Now it's working well.

Tks again.

To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages