无法使用本地express.js从mlab获取数据

时间:2017-01-25 16:48:43

标签: node.js mongodb mongoose mlab

我试图从我当地的express.js上取得mLab。节点编译正确,但是当我使用mongoose访问mLab时,控制台显示500错误。回调中的错误对象也为空。

路由器/ index.js

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

var post = require('../models/post');

/* GET home page. */
router.get('/', function(req, res, next) {
    post.getAllPosts(function(err){
        if(err) throw err;
        res.render('index', {posts : posts} );
    },3);
});

module.exports = router;

模型/ post.js

var mongoose = require('mongoose');

//Post Schema
var PostSchema = mongoose.Schema({
    title : {
        type: String
    },
    body : {
        type: String
    },
    category:{
        type:String
    },
    date: {
        type:Date
    },
    author:{
        type:String 
    },
    mainimage:{
        type:String
    }
});

var Post = module.exports = mongoose.model("Post", PostSchema);

//get all post
module.exports.getAllPosts = function(callback){
    Post.findOne(callback);
}

然后这是mLab集合架构

{
    "_id": {
        "$oid": ""
    },
    "title": "test1",
    "body": "test body",
    "category": "test categ",
    "date": {
        "$date": "2017-01-24T16:24:23.187Z"
    },
    "author": "author",
    "mainimage": "testimg"
}
访问/路由后

console.log

my-MacBook-Air: $ node app.js
GET / 500 353.624 ms - 5709
GET /vendor/bootstrap/css/bootstrap.min.css 304 3.488 ms - -
GET /stylesheets/clean-blog.min.css 304 10.164 ms - -
GET /vendor/font-awesome/css/font-awesome.min.css 304 9.533 ms - -
GET /vendor/jquery/jquery.js 304 8.806 ms - -
GET /vendor/bootstrap/js/bootstrap.min.js 304 8.019 ms - -
GET /js/jqBootstrapValidation.js 304 6.361 ms - -
GET /js/contact_me.js 304 5.368 ms - -
GET /js/clean-blog.min.js 304 2.721 ms - -
GET /images/home-bg.jpg 304 7.196 ms - -
GET /vendor/font-awesome/fonts/fontawesome-webfont.woff2?v=4.6.3 304 1.484 ms - -

0 个答案:

没有答案