0

I have this as a server.js:

var express = require('express'),
  app = express(),
  port = process.env.PORT || 3000,
  mongoose = require('mongoose'),
  Task = require('./api/models/toDoListModel'),
  bodyParser = require('body-parser');

mongoose.Promise = global.Promise;
mongoose.createConnection('mongodb://localhost:27017/Tododb'); 


app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


var routes = require('./api/routes/toDoListRoutes');
routes(app);

app.listen(port);

And when I use posman to post/get, I get no response from the server (on a correct key-value) although localhost:27017 is running and I created a Tododb db using Robo 3T. But when I don't enter any key/value in postman, I get a response. Am I missing something? All addresses are correct.

1
  • .createConnection() does not actually "connect". You need to initiate the connection. See Connections in the documentation Commented Oct 23, 2017 at 4:05

1 Answer 1

2

You can connect to MongoDB with the mongoose.connect() method.

mongoose.connect('mongodb://localhost:27017/Tododb');

If you don't call mongoose.connect() then mongoose.connection doesn't contain an an open connection. You should be using the return value from your mongo.createConnection() call instead (that you've to saved into db var and use that object).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Mongodb was prompting a deprecation warning and I removed the .connect command. My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.