简体   繁体   English

使用Mongoose从MongoDB检索数据不起作用

[英]Retrieving data from MongoDB using Mongoose not working

I have a simple NodeJS, Express, Mongoose app to save and retrieve data from MongoDB based on an online tutorial. 我有一个简单的NodeJS,Express,Mongoose应用程序,可以根据在线教程从MongoDB中保存和检索数据。 Problem I am facing is that whenever I try to retrieve any saved record, I only get _id and __v as shown below...all other movie fields are not displayed. 我面临的问题是,每当尝试检索任何已保存的记录时,我只会得到_id和__v,如下所示……其他所有电影字段都不会显示。 Below is the code I am using to save / retrieve data to/from MongoDB can someone please help by tellin me what I am doing wrong here and how to fix it? 以下是我用来在MongoDB中保存数据/从MongoDB中检索数据的代码,请有人告诉我我在这里做错了什么以及如何解决它? Thanks 谢谢

{
    "_id": "542819b25550346209c85272",
    "__v": 0
}

app.js: app.js:

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var movies = require('./routes/movies');
var app = express();
var dbName = 'movieDB';
var connectionString = 'mongodb://localhost:27017/' + dbName;

mongoose.connect(connectionString);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use('/api', movies);
 
module.exports = app;

movie.js movie.js

var mongoose=require('mongoose');
var Schema=mongoose.Schema;
var movieSchema = new Schema({
  title: String,
  releaseYear: String,
  director: String,
  genre: String
});
module.exports = mongoose.model('Movie', movieSchema);

movies.js 电影.js

var Movie = require('../models/movie');
var express = require('express');
var router = express.Router();

router.route('/movies').get(function(req, res) {
  Movie.find(function(err, movies) {
    if (err) {
      return res.send(err);
    }
    res.json(movies);
  });
});
router.route('/movies').post(function(req, res) {
  var movie = new Movie(req.body); 
  movie.save(function(err) {
    if (err) {
      return res.send(err);
    }
    res.send({ message: 'Movie Added' });
  });
});
module.exports = router;

I have tested saving to the DB using Chrome Postman where I have sent the following data as POST > ROW in JSON format to /api/movies: 我已经测试过使用Chrome Postman保存到数据库的功能,在其中我已将POST> ROW以下数据以JSON格式发送到/ api / movies:

 {"title":"Matrix Reloaded 2", "releaseYear": "2015", "director": "Mike", "genre": "Action"}

A really important part of negotiating "truly" RESTful sevices is that the "Content-Type" header setting should be appropriate to the payload. 协商“真正的” RESTful服务的一个非常重要的部分是“ Content-Type”标头设置应适合有效负载。

So when sending JSON content in the body of your request, the "Content-Type" header value should be set to "application/json". 因此,在请求正文中发送JSON内容时,“ Content-Type”标头值应设置为“ application / json”。

This is so that "well behaved" de-serialize engines actually understand that they have that particular content to act on. 这样一来,“行为良好”的反序列化引擎就会真正理解它们具有要执行的特定内容。 Bad implementations "assume" that they are getting JSON or XML or some other format. 错误的实现会“假设”他们正在获取JSON或XML或其他某种格式。 But good implementations inspect that header value before doing anything. 但是良好的实现会在执行任何操作之前检查该标头值。

Regardless, try to set this from your client whenever talking to a RESTful interface. 无论如何,无论何时与RESTful接口进行通信,都应尝试从客户端进行设置。 It should be doing the right thing, even if someone codes it up incorrectly. 即使有人编码不正确,它也应该做正确的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM