我在mongolab和集合名称'login'中创建了db'Users',并从我的本地机器nodejs代码中搜索了我的名字。我能够连接到db但无法获取任何细节,我是在res.json(doc)中获取NULL。 我是mongoose和node.js的新手,我很感激有人指导我如何做到这一点
这是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);
以下是login.js的代码
var mongoose = require('mongoose');
var LoginSchema = new mongoose.Schema({
name: String,
email:String,
number: Number
});
module.exports = mongoose.model('login', LoginSchema);
这是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;
在Mongolab我的收藏'登录'只包含1个数据
{ “_id”:“56e26f73592f88fc1a989b6f”, “名字”:“p”, “email”:“a”, “数字”:“0” }
答案 0 :(得分:0)
Mongoose自动复数集合。您需要logins
login.find
集合中的文档才能返回任何内容。