estoy intentando extraer datos de mongoDb, con ( moongose , nodejs )... ya me he conectado correctamente a la base de datos, y ya tengo una collection creada. para esa collection, tengo 1 model en el file ( models/candidates.js ) :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const candidatesSchema = new Schema({
Login: String,
BehaviorPoints: Number,
Note: String
});
const Candidates = mongoose.model('Candidates', candidatesSchema);
module.exports = Candidates;
entonces estoy intentando usar ese modelo desde ( server.js ) file :
/* Database */
const mongoose = require('mongoose');
const { Candidates } = require('./models/candidates');
Here is my connection to the database:
async _connectDB() {
const dbUser = process.env.DBUSER;;
const dbPassword = process.env.DBPASSWORD;
const dbName = process.env.DBNAME;
const dbUri = `mongodb+srv://${dbUser}:${dbPassword}@********.dzae0.mongodb.net/${dbName}?retryWrites=true&w=majority`;
mongoose.connect(dbUri, { useNewUrlParser: true, useUnifiedTopology: true } )
.then( () => console.log(clc.cyan('Debug: Database connected successfully')))
.catch(e => console.log(e));
}
y esta es la forma en la que estoy intentando extraer esa informacion:
app.get("/candidates", async (req, res, next) => {
if (req.originalUrl.startsWith("/404")) {
return error();
}
try {
const arrayCandidatesDB = await Candidates.find();
res.send(arrayCandidatesDB);
}
catch(err) {
console.log(clc.red(err));
}
});
El error que obtengo es el siguiente:
Debug: Server listening on port 3001
Debug: Database connected successfully
TypeError: Cannot read properties of undefined (reading 'find').