Express и MongoDB - не возвращаются детали коллекции - PullRequest
0 голосов
/ 13 июля 2020
• 1000 / colors, но когда я пытаюсь сделать то же самое для фруктов, он просто возвращает []. Можно ли маршрутизировать две разные коллекции на одном сервере express? Опять же, я новичок в этом, поэтому приветствую любые отзывы! Код ниже.

Express сервер:

import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';

import Color from './models/color'
import Fruit from './models/fruit'

const app = express();
const router = express.Router();

app.use(cors());
app.use(bodyParser.json());

mongoose.connect('mongodb://localhost:27017/sandbox');

const connection = mongoose.connection

router.route('/colors').get((req, res) => {
    Color.find((err, colors) => {
        if (err) 
            console.log(err);
        else {
            res.json(colors);
        }
    });
});

router.route('/fruits').get((req, res) => {
    Fruit.find((err, fruits) => {
        if (err) 
            console.log(err);
        else {
            res.json(fruits);
        }
    });
});

app.use('/', router);

app.listen(4000, () => console.log("The Express Server is running"));

Цветовая модель:

import mongoose from 'mongoose'

const Schema = mongoose.Schema;

let Color = new Schema({
    colorName: {type: String}, 
    uniqueId: {type: String},
    colorShade: {type: String}, 
    colorHex: {type: String}
});

export default mongoose.model('Color', Color);

Модель фруктов:

import mongoose from 'mongoose'

const Schema = mongoose.Schema;

let Fruit = new Schema({
    fruitName: {type: String}, 
    uniqueId: {type: String},
    fruitColor: {type: String},
    fruitRipeness: {type: String},
    fruitFamily: {type: String},
});

export default mongoose.model('Fruit', Fruit);
...