I try to connect an application to mongoDB with express, for the moment i have no problem with the collection users, it works perfectly but it doesn't work for my second collection, when i try to get all data of my second collection, express return me a empty array.
// Import dependencies
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();
// MongoDB URL from the docker-compose file
const dbHost = 'mongodb://database/mybase';
// Connect to data
mongoose.connect(dbHost);
// create mongoose schema
const userSchema = new mongoose.Schema({
mail: String,
mdp: String,
firstname: String,
participants: Number
});
// create mongoose schema
const roomSchema = new mongoose.Schema({
roomId: String,
});
// create mongoose model
const User = mongoose.model('User', userSchema);
const Room = mongoose.model('Room', roomSchema);
/* GET api listing. */
router.get('/', function(req, res) {
res.send('api works');
});
/* GET all users. */
router.get('/users', function(req, res) {
User.find({}, function (err, users) {
if (err) {
res.status(500).send(error)
}
res.status(200).json(users);
});
});
/* GET all rooms. */
router.get('/room', function(req, res) {
Room.find({}, function (err, room) {
if (err) {
res.status(500).send(error)
}
res.status(200).json(room);
});
});
module.exports = router;
In my mongo data are stored like this :
> db.users.find()
{ "_id" : ObjectId("5903a7ee0e999a5054cc402b"), "mail" : "mail@google.com", "mdp" : "toto", "firstname" : "firstname", "participants" : 100 }
{ "_id" : ObjectId("5903ae7952163fd952970ed5"), "mail" : "mail2@google.com", "mdp" : "toto", "firstname" : "firstname", "participants" : 50 }
> db.room.find()
{ "_id" : ObjectId("59043f280d062a4959b69315"), "roomId" : "6733454" }
I search for a long time but to be honest, i don't know why it doesn't work, i also try to put users data into room data and to copy/paste user schema into room schema, but it doesn't work also.
If i put the User model object in router.get room, it works, i get users data, so for me it's not a problem of route.
via djdelarue
No comments:
Post a Comment