In the MongoDB Shell, MongoAtlas, and MongoDB compass, I can see I have 9 records in my fields collection. I'm able to search for them and find them there, however when I try to access them through my JS where I want, it returns nothing.
I know it's probably something I'm missing, because if I do a find({}) right as my code starts up, it does return the objects, just not in my controller. I'm getting an error in my HTML file saying it can't iterate on an undefined object.
Router
const express = require('express');
const router = express.Router();
const fieldInstances = require('../controllers/fieldInstances');
router.route('/').get(fieldInstances.index).post(fieldInstances.createFieldInstance);
Controller
const Field = require('../models/field');
module.exports.index = async (req, res) => {
const fields = Field.find({});
res.render('fieldInstances/index', {fields});
};
<div class="modal-body d-flex">
<form action="/formInstance/new" method="GET">
<% for (let field in fields) { %>
<input type="checkbox" class="btn-check" id="btn-check-outlined" autocomplete="on">
<label class="btn btn-outline-secondary" for="btn-check-outlined" name= "fieldInstance[field]"><%= field.name %></label><br>
<% } %>
<button type="submit" class="btn btn-success">To the Form!</button>
</form>
</div>
model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const FieldSchema = new Schema ({
name: {
type: String,
required: true,
unique: true,
enum: ['Integer', 'Number', 'Money', 'File Upload', 'Radio Button', 'Single Select', 'Multi-Select', 'Checkbox', 'Single User', 'Multi-User']
},
bounds: new Schema({ any: Schema.Types.Mixed })
});
module.exports = mongoose.model('Field', FieldSchema);
Thanks(:
UPDATE: Database connection --
const express = require('express');
const mongoose = require('mongoose');
const port = 3000;
mongooseConnect().catch(err => console.log(err));
async function mongooseConnect() {
await mongoose.connect("mongodb+srv://amillerlite:password@projects.brxq6oa.mongodb.net/?retryWrites=true&w=majority");
};
I've gotten it to start returning something, but that something is a like 50 undefineds. Its almost like find({}) isn't returning what it's supposed to.