简体   繁体   中英

Not able to access collection in mongolab from my local node.js server

I have made db 'Users' in mongolab and a collection name 'login' and from my local machine nodejs code I am searching the name.I am able to connect to the db but not able to fetch any details,I am getting NULL in res.json(doc). I am new in mongoose and node.js,I would appreciate anyone guiding me on how to do this

Here is code of server.js

var express  = require('express');
var app      = express();                               // create our app w/ 
var mongoose = require('mongoose');         // mongoose for mongodb
var port     = process.env.PORT || 3000;                // set the port
var database = require('./config/database');
var routes = require('./routes/routes'); 
var morgan   = require('morgan');
var bodyParser = require('body-parser');
var http = require('http');
var methodOverride = require('method-override');

var routes= require('./routes/routes');

mongoose.connect(database.url, function(err) {
if(err) {
    console.log('connection error', err);
} else {
    console.log('connection successful');
    }
});

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

app.post('/login',routes);//for submit button 

app.listen(port);
console.log("App listening on port " + port);

Here is code of login.js

var mongoose = require('mongoose');

var LoginSchema = new mongoose.Schema({
   name: String,
   email:String,
   number: Number
});

 module.exports = mongoose.model('login', LoginSchema);

Here is code of routes.js

 var express = require('express');
 var router = express.Router();

  var mongoose = require('mongoose');
  var login = require('../model/login.js');


 router.post('/login', function(req, res, next) {
   console.log("i am going to login");
    console.log(req.body);
   var s = req.body.name;
  login.find({'name':s},function(err,doc){
   console.log(doc);
   res.json(doc);
  });

  });

 module.exports = router;

And in Mongolab my collection 'login' contains only 1 data

{ "_id": "56e26f73592f88fc1a989b6f", "name": "p", "email": "a", "number": "0" }

Mongoose automatically pluralizes collections. You'll need a document in the logins collection for login.find to return anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM