【发布时间】:2018-10-16 08:31:44
【问题描述】:
mongodb 数据库使用 mongoose 和 express js 的问题,跟随教程
教程可以在这里找到Tutortial Angular6 MEAN Stack
我的 server.js 文件内容
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import Issue from './models/Issue';
const app = express();
const router = express.Router();
app.use(cors());
app.use(bodyParser.json());
mongoose.connect('mongodb://localhost:27017/issues', { useNewUrlParser: true });
const connection = mongoose.connection;
connection.once('open', () =>{
console.log("MongoDB connection established successfully");
});
router.route('/issues').get( (req, res) => {
Issue.find((err,issues) => {
if(err)
console.log(err);
else
res.json(issues);
});
});
我的Issue.js文件内容
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
let Issue = new Schema ({
title: {
type: String
},
responsible: {
type: String
},
description: {
type: String
},
severity: {
type: String
},
status: {
type: String,
default: 'Open'
}
});
export default mongoose.model('Issue', Issue);
我的问题集合也包含有效的 json 数据,但是当我尝试访问 url localhost:4000/issues 时,我得到的只是一个空 []
这也是我对 data/db 的权限
ls -ail
total 12
656701 drwxrwxrwx 3 root root 4096 oct 15 18:56 .
2 drwxr-xr-x 25 root root 4096 oct 15 18:56 ..
658216 drwxrwxrwx 4 dan root 4096 oct 16 11:29 db
有人能指出我正确的方向吗? 谢谢
【问题讨论】:
-
“问题”集合中有数据吗?
-
是的,我在问题集合中有示例数据
-
Issue != issue(区分大小写很重要)。
-
我已经验证过了,如果我也发布我的 mongodb 和一些示例数据会有帮助吗?
-
是的。最好的办法是从连接、选择数据库到读取问题集合(命令和输出)的命令行日志。
标签: node.js mongodb ecmascript-6 babeljs