我使用mongoose和express库调用API来获取mongodb数据。
如果我放弃使用foreach (Control control in tabPage1.Controls.OfType<Button>())
{
MessageBox.Show(control.GetType().ToString());
if (control.Name first letters == "indicatorDi")
{
control.BackColor = Color.LawnGreen;
}
}
Schema
如果我使用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);
});
});
}
这是我的Shecma代码:
module.exports = {
gretting (req, res, next) {
// I set the collection name is Tainan
Driver.find()
.then(movie => res.send(movie))
.catch(next);
}
这是我的主文件app.js使用promise:
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;
任何人都知道如何在我的案例中获取我的收藏数据?
此外,如果我想获取指定的字段数据,我该怎么办?
我尝试这段代码,但它仍然是一个空对象
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;
任何帮助将不胜感激。提前谢谢。