我正在研究一个可以从mongodb获取预制数据的rest api,但由于某种原因它没有得到已经在mongodb中的数据。当我输入localhost:3000 / film时,它根本不会返回任何数据。它返回[],就是这样。我检查了mongodb,数据已经存在。
我使用的命令是:
蒙戈
使用FilmTest
展示收藏品
db.Film.find()
它以json格式返回其中的所有数据,因此测试数据已经存在,但是当我在启动节点server.js时查看/ film时,其余的api没有返回它。
server.js
var express = require('express');
var mongoose = require('mongoose');
var cors = require('cors');
mongoose.connect('mongodb://localhost:27017/FilmTest')
var filmSchema = ({
categoryId: String,
genre: String,
title: String,
releaseDate: String,
rating: Number,
director: String,
cast: String,
production: String,
description: String,
plot: String,
opinion: String,
isSpecial: String,
imageS: String,
imageL: String
});
var Film = mongoose.model('Film', filmSchema, 'film')
var app = express()
app.use(cors());
app.get('/film', function(req, res) {
Film.find(function (err, doc) {
res.send(doc);
})
})
app.listen(3000);
答案 0 :(得分:1)
试试这个:
var express = require('express');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var cors = require('cors');
mongoose.connect('mongodb://localhost:27017/FilmTest')
var filmSchema = new Schema({
categoryId: String,
genre: String,
title: String,
releaseDate: String,
rating: Number,
director: String,
cast: String,
production: String,
description: String,
plot: String,
opinion: String,
isSpecial: String,
imageS: String,
imageL: String
});
var Film = mongoose.model('Film', filmSchema, 'film')
var app = express()
app.use(cors());
app.get('/film', function(req, res) {
Film.find({}, function (err, doc) {
res.send(doc);
})
})
app.listen(3000);