Данные не извлекаются из mon go database.receiving пустая ошибка. Хотя сбор присутствует в mon go cloud - PullRequest
0 голосов
/ 12 апреля 2020

Я не могу получить данные через мой запрос на получение. Он возвращает пустой массив, хотя, когда я проверяю сервер Mon go Cloud, он имеет всю коллекцию. Я использую тот же поток и код для другой коллекции «ПОЛЬЗОВАТЕЛИ» той же БД, и это работает нормально. ошибка Я предоставил файлы подключения Router => model =>. [введите описание изображения здесь] [1] соединение. js

const Schema = mongoose.Schema;
mongoose.set('useCreateIndex', true)

const url = process.env.ATLAS_URI;
mongoose.connect(url, { useNewUrlParser: true }
);

const connection = mongoose.connection;
connection.once('open', () => {
    console.log("MongoDB database connection established successfully");
})


let userSchema = new Schema({
    username: {
        type: String,
        required: true,
        unique: true,
        trim: true,
        minlength: 3
    },
}, {
    timestamps: true,
}, { collection: 'users' });


let exerciseSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true,
    },
    duration: {
        type: Number,
        required: true
    },
    date: {
        type: Date,
        required: true
    }
}, {
    timestamps: true,
}, { collection: 'exercises' });


var collection = {}

collection.getUser = () => {
    return mongoose.connect(url, { useNewUrlParser: true })
        .then((database) => {
            return database.model('users', userSchema)
        }).catch((err) => {
            return (err + 'Connection couln\'t be established to user database')
        })
}

collection.getExercise = () => {
    return mongoose.connect(url, { useNewUrlParser: true })
        .then((database) => {

            return database.model('exercises', exerciseSchema)
        }).catch((err) => {
            return (err + 'Connection couln\'t be established to exercise database')
        })
}
module.exports = collection;```


  [1]: https://i.stack.imgur.com/moT2z.png

1 Ответ

0 голосов
/ 12 апреля 2020

Используйте необходимость использовать запрос find или findOne

return database.model(...).find({}).exec();

Для получения дополнительной информации https://mongoosejs.com/docs/queries.html

...