mongodb不返回任何结果

发布于 2025-02-13 18:21:14 字数 2616 浏览 1 评论 0原文

在Mongodb Shell,Mongoatlas和Mongodb指南针中,我可以看到我的田野收藏中有9个唱片。我可以搜索它们并在那里找到它们,但是当我尝试通过我想要的JS访问它们时,它却一无所获。

我知道这可能是我缺少的,因为如果我在代码启动时正确地找到({}),它确实会返回对象,而不是在控制器中。我在HTML文件中遇到了一个错误,说它不能在未定义的对象上迭代。

路由器

const express = require('express');
const router = express.Router();
const fieldInstances = require('../controllers/fieldInstances');

router.route('/').get(fieldInstances.index).post(fieldInstances.createFieldInstance);

控制器

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>

模型

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);

谢谢(:

更新:数据库连接 -

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:[email protected]/?retryWrites=true&w=majority");
};

我已经开始返回某些东西,但是它是50个未定义的东西。它几乎就像find({})没有返回应该返回它的内容。

< “在此处输入图像说明”

“在此处输入图像描述”

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:[email protected]/?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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文