Question

I am trying to learn MongoJS, But its not working.
I wrote this code so far -

/* Basics */

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    db = require("mongojs").connect("mydb", ["users"]);

server.listen(27017, null);

io.set('transports', ['xhr-polling']);

// routing
app.get('/', function (req, res) {
    res.sendfile("index.html");
    app.use(express.static(__dirname));
});

db.users.save({username : "admin"}, function(err, saved) {
    if( err || !saved ) { console.log("User not saved"); }
    else { console.log("User saved"); }
});

It log to the console "User not saved", but why? what I did wrong?


Thanks in Advance

Was it helpful?

Solution

You are confused about ports and are trying to tell your web server to listen on port 27017, which is the port mongodb listens on. Try server.listen(3000) (or another available port of your choosing). If you want to troubleshoot the db.users.save error, try printing the actually error message (err) to console and go from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top