1
我已經在mongolab和集合名稱'login'中創建了db'Users'並且從本地計算機nodejs代碼中搜索了名稱。我能夠連接到數據庫但無法獲取任何細節,我在res.json(doc)中獲得了NULL。 我在貓鼬和node.js中新的,我希望有人指導我如何做到這一點無法從我的本地node.js服務器訪問mongolab中的集合
這裏是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我的收藏 '登錄',包含3碼NS只有1個數據
{ 「_id」: 「56e26f73592f88fc1a989b6f」, 「名稱」: 「P」, 「電子郵件」: 「一」, 「號」: 「0」 }