猫鼬不保存数据 [英] Mongoose Not Saving Data

查看:67
本文介绍了猫鼬不保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库上进行简单查询时遇到麻烦.遵循本教程: https://scotch.io /tutorials/build-a-restful-api-using-node-and-express-4 ,当调用Model.find()时,他会收到一个带有名称字段(唯一的自定义字段)的JSON对象,并且_id和__v.当我做同样的事情时,我收到的全部是_id和__v字段.我确实收到了一个成功的回复,说帖子是创建的,但其中不包括标题或内容字段.但是查询显示数据从未保存过.

I am having trouble with a simple query on my database. Following this tutorial: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 when Model.find() is called, he receives a JSON object back with the name field (the only custom field) and the _id and __v. When I do the same, all I receive back is the _id and __v field. I do get a successful response back saying the post was created, but it doesn't include the title or content fields. Yet a query shows that the data was never saved.

路由和查询:

var express = require("express");
var router = express.Router();
var Post = require("../app/models/post.js");

/* Drop Post collection
Post.remove({}, function(err, num_docs) {
    if (err) {
        res.send(err);
    } else {
        console.log("Collection dropped, documents deleted: " + num_docs);
    }
});
*/

// Middleware for all routes.
router.use(function(req, res, next) {
    console.log("API request made.");
    next(); // Go to next routes, don't stop here
});

// Test route to ensure routing is working
router.get("/", function(req, res) {
    res.json({
        message: "Hooray! Welcome to the API!"
    });
});

// On routes that end in /posts
router.route("/posts")
    // Create post. (Accessed at POST http://localhost/api/posts)
    .post(function(req, res) {

        var post = new Post(); // Create new instance of post model

        post.title = req.body.title; // Set title (from request)
        post.content = req.body.content; // Set content (from request)

        // Save the post, and check for errors.
        post.save(function(err) {
            if (err) {
                res.send(err);
            } else {
                res.json({
                    message: "Post created!",
                    title: post.title,
                    content: post.content
                });
            }
        });
    })

    .get(function(req, res) {
        Post.find({}).exec(function(err, posts) {
            if(err) {
                res.send(err);
            } else {
                res.json(posts);
            }

        });
    });

module.exports = router;

响应:

[
    {
        "_id": "56a6adc31f06c4dc1cf82888",
        "__v": 0
    },
    {
        "_id": "56a9768888f806dc1fe45415",
        "__v": 0
    },
    {
        "_id": "56a97f3f4e269b7c21311df8",
        "__v": 0
    }
]

shell中的db查询返回相同的信息,只是_id和__v字段.

A db query in the shell returns the same information, just an _id and __v field.

推荐答案

我现在很困惑.它突然起作用,代码与上面的代码完全相同.如果有一天有人偶然发现它并且可以解决这个谜团,我将把它保持打开状态.

I am beyond confused right now. It suddenly works, the code is the exact same as above. I am going to leave this open in case someone stumbles across it one day and can solve this mystery.

这篇关于猫鼬不保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆