MongoDb和POSTMAN:文档未添加到集合中

时间:2019-06-18 09:36:11

标签: mongodb http mongoose https postman

我刚开始使用mongodb。
我已经设置了所有mongodb和mongoose配置,它们可以正常工作。
以下是项目文件:

  

server.js:

const TableRow = require('./models/tableRow');
const bodyParser = require('body-parser');
const cors = require('cors')
const express = require('express');
const mongoose= require('mongoose')
const app = express();
const router = express.Router();
app.use(cors());
app.use(bodyParser.json());
mongoose.connect('mongodb://localhost/table', function(err) {
  if (err) { throw err; }
  console.log('Successfully connected');
});
const connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', () => {
    console.log('MongoDB database connection established successfully!');
});
app.use('/', router);
router.route('/table/add').post((req, res) => {
  let tableRow = new TableRow (req.body);
  tableRow.save()
      .then(issue => {
          res.status(200).json({'tableRow': 'Added successfully'});
      })
      .catch(err => {
          res.status(400).send('Failed to create new record');
      });
});
app.listen(5757, () => console.log(`Express server running on port 5757`)); 
  

tableRow.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema;
let TableRow = new Schema({
    column1Data: {
        type: String
    },
    column2Data: {
        type: String
    }
    });
module.exports = mongoose.model('TableRow', TableRow);

当我尝试使用POSTMAN进行测试时:
enter image description here

正如您在响应正文中看到的那样,我得到了这个信息:

  

{       “ tableRow”:“添加成功”}

自从server.js开始,我有以下代码:

router.route('/table/add').post((req, res) => {
  let tableRow = new TableRow (req.body);
  tableRow.save()
      .then(issue => {
          res.status(200).json({'tableRow': 'Added successfully'});
      })
      .catch(err => {
          res.status(400).send('Failed to create new record');
      });
});

我认为应该做的工作。但是,当我键入:

  

db.table.find()

我看到表是空的。知道为什么吗?
谢谢!

0 个答案:

没有答案