0

I'm a little confused by this part of mongoose, currently i am trying create the habit of structuring my project(using express-generator). I have these part of codes and i am trying to make a get request and return some value from the mongo but in my mind i am not reaching it right.

The app.js is basically the default when i run it for the first time, but to be clear i have line below for my route to work.

app.get('/login', usersRouter);

Then I have the users.js in the routes folder

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');

//Connect to localhost
mongoose.connect('mongodb://localhost:27017/LCC');

//Bring models
let User = require('../models/user-model');

router.get('/login', function (req, res) {

  User.find({}, function(err, result){
    console.log(result);
  });
});

And my model in the other folder:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//User Schema
var userProfile = new Schema({
    username: String,
    password: String
});

module.exports = mongoose.model('Users', userProfile);

My question is, what did i missed? Because in my mind it was supposed to work the console.log and retrieve all the users in mongo. But i only get [] in the terminal (there are six records in the database).

I just copied one route and not all of the code but if something is missing just tell me and i'll edit the post, with mongo driver i can do these queries but i am trying to learn this way by my own. And english is not my first language so sorry for any mistakes.

1 Answers1

0
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/dbname");

var userSchema = mongoose.Schema({
  name: String,
  age: Number,
});

module.exports = mongoose.model("module", userSchema);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Aug 02 '22 at 09:32