0

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.

enter image description here

enter image description here

  • Did you connect to the database? I don't see that anywhere in your code. – asportnoy Jul 07 '22 at 22:45
  • I did, yes. And I'm getting something back from the DB now, but it doesn't make any sense to me. I updated with a few pictures and the connection I used. – amiller_lite Jul 07 '22 at 23:25
  • You don't seem to be waiting for results in your async controllers. Did you mean to use `const fields = await Field.find({})`? See https://mongoosejs.com/docs/api.html#model_Model.find – Phil Jul 07 '22 at 23:58
  • I had originally had the await in there, I must have pulled it out while trying to troubleshoot. – amiller_lite Jul 08 '22 at 00:00
  • Also, don't use `for..in` for arrays. Use `for (let field of fields)` – Phil Jul 08 '22 at 00:01
  • for (let field of fields) was giving me an error because fields was not iterable. – amiller_lite Jul 08 '22 at 00:06
  • Did you add the `await` back in? If it's still not working, start debugging... `const fields = await Fields.find(); console.log("fields", fields);` – Phil Jul 08 '22 at 00:47

0 Answers0