猫鼬只保存_id和_v

时间:2019-06-25 23:17:28

标签: node.js mongodb express mongoose backend

这确实是重复的question,但是没有答案。

问题是,当我通过发布请求用猫鼬保存新记录时,保存的内容如下:

{ "_id" : ObjectId("5d11590975c82f216eaa4712"), "__v" : 0 }

我正在遵循此tutorial,因此代码应该可以正常工作,但是无论它在哪里:

猫鼬模式:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let Todo = new Schema({
    todo_description: {
        type: String
    },
    todo_responsible: {
        type: String
    },
    todo_priority: {
        type: String
    },
    todo_completed: {
        type: Boolean
    }
});
module.exports = mongoose.model('Todo', Todo);

代码:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const todoRoutes = express.Router();
const PORT = 4000;

let Todo = require('./todo.model');

app.use(cors());
app.use(bodyParser.json());

mongoose.connect('mongodb://127.0.0.1:27017/todos', { useNewUrlParser: true });
const connection = mongoose.connection;

connection.once('open', function() {
    console.log("MongoDB database connection established successfully");
})

todoRoutes.route('/').get(function(req, res) {
    Todo.find(function(err, todos) {
        if (err) {
            console.log(err);
        } else {
            res.json(todos);
        }
    });
});

todoRoutes.route('/:id').get(function(req, res) {
    let id = req.params.id;
    Todo.findById(id, function(err, todo) {
        res.json(todo);
    });
});

todoRoutes.route('/update/:id').post(function(req, res) {
    Todo.findById(req.params.id, function(err, todo) {
        if (!todo)
            res.status(404).send("data is not found");
        else
            todo.todo_description = req.body.todo_description;
            todo.todo_responsible = req.body.todo_responsible;
            todo.todo_priority = req.body.todo_priority;
            todo.todo_completed = req.body.todo_completed;

            todo.save().then(todo => {
                res.json('Todo updated!');
            })
            .catch(err => {
                res.status(400).send("Update not possible");
            });
    });
});

todoRoutes.route('/add').post(function(req, res) {
    let todo = new Todo(req.body);
    todo.save()
        .then(todo => {
            res.status(200).json({'todo': 'todo added successfully'});
        })
        .catch(err => {
            res.status(400).send('adding new todo failed');
        });
});

app.use('/todos', todoRoutes);

app.listen(PORT, function() {
    console.log("Server is running on Port: " + PORT);
});

发布请求: post request 获取请求get request

要确认这是mongodb中的输出: mongodb

4 个答案:

答案 0 :(得分:1)

问题在于主体需要是Json(application / json)而不是Text

enter image description here

答案 1 :(得分:0)

您的代码一切都很好

只需将此行添加到您的todo.model.js

module.exports = mongoose.model('Todo', Todo);

答案 2 :(得分:0)

Dashboard

1

编辑:

我也遇到了这个问题,我修复了 Content-type: application / json 并且它工作了

答案 3 :(得分:0)

确保将 app.use(express.json()) 添加到 server.js 文件中。