I use mongoose and express library to call API to get mongodb data.
This code i use API can get the data as well if i give up to use Schema
const Driver = require('../models/driver');
const mongoose = require('mongoose');
// If i don't use Schema by using connection can get collection data
const connection = mongoose.connection;
module.exports = {
gretting (req, res, next) {
const { theater } = req.query;
connection.db.collection(theater, (err, collection) => {
collection.find({}).toArray((err, data) => {
res.send(data);
});
});
}
If i use Schema, this code i try i will get an empty object
module.exports = {
gretting (req, res, next) {
// I set the collection name is Tainan
Driver.find()
.then(movie => res.send(movie))
.catch(next);
}
Here is my Shecma code:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PointSchema = new Schema({
type: { type: String, default: 'Point'},
coordinates: { type: [Number], index: '2dsphere'}
});
const MovieSchema = new Schema({
theater: String,
phone: String,
movie: {
film: {
moviePhoto: [],
movieStills: [],
movieActorPhoto: [],
movieActorCn: [],
movieDate: [],
versionType: [],
cnName: [],
movieTime: [],
movieContent: [],
movieStyle: [],
releasedTime: [],
enName: []
}
},
theaterPhoto: String,
address: String,
theaterCn: String,
geometry: PointSchema
});
const Driver = mongoose.model('Tainan', MovieSchema);
module.exports = Driver;
This my main file app.js using promise:
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./routes/routes');
const app = express();
mongoose.Promise = global.Promise;
if (process.env.NODE_ENV !== 'test') {
mongoose.connect('mongodb://localhost/movies');
}
app.use(bodyParser.json());
routes(app);
app.use((err, req, res, next) => {
res.status(422).send({ error: err.message });
});
module.exports = app;
Any one knows how to get my collection data in my case ?
Also if i want to get the specified field data what should i do ?
I try this code but its still an empty object
module.exports = {
gretting (req, res, next) {
Driver.find({ "theater": "TodayTainan" })
.then(movie => res.send(movie))
.catch(next);
}
Any help would be appreciated. Thanks in advance.