【问题标题】:Problem with a mongodb database using mongoose and express js, following a tutorial遵循教程,使用 mongoose 和 express js 的 mongodb 数据库出现问题
【发布时间】: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


【解决方案1】:

正如@str 提到的,mongodb 数据库区分大小写,所以我在 mongo 中创建数据库时犯了一个错误。

我将我的数据库命名为 Issues 而不是 issues,因此我不得不使用以下命令来解决它:

mongodump -d Issues -o mongodump/
use Issues
db.dropDatabase()
mongorestore -d issues mongodump/Issues

感谢您的正确指点@str

【讨论】:

    猜你喜欢
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2022-01-15
    • 2019-01-02
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多