来自另一个文件的 Mongoose 模型 returns 未定义
Mongoose model from another file returns undefined
我正在尝试在 nodejs 中编写 MVC。问题是,渲染方法 returns 未定义,而不是数据库数据。但是如果我打电话给
Website.find({}, function(err, docs) {
res.send(`docs`);
})
直接来自 index.js - 它工作正常,我做错了什么吗?
index.js:
app.get('/websites', (req, res) => {
const View = require("./views/websitesView");
const Controller = require("./controllers/websitesController");
const Model = require("./models/websiteModel");
var model = new Model();
var controller = new Controller(model);
var view = new View(controller);
res.send(view.render(req, res));
})
view.js:
var controller = require("../controllers/websitesController");
class websitesView {
controller;
constructor(controller) {
this.controller = controller;
}
setTab(tab) {
this.tab = tab;
}
render(req, res) {
res.send(`${this.controller.getAllWebsites()}`);
}
}
module.exports = websitesView;
controller.js:
const Website = require("../models/websiteModel");
class websitesController {
model;
constructor(model) {
this.model = model;
}
getAllWebsites() {
Website.find({}, function(err, docs) {
return docs;
})
}
}
module.exports = websitesController;
model.js
const mongoose = require("mongoose")
const websiteSchema = new mongoose.Schema({
url: String,
user_id: String,
type: String,
status: Number,
timestamp: Date
}, {collection: "aaa_websites"})
module.exports = mongoose.model("Website", websiteSchema, 'aaa_websites')
Using a Mongoose model defined in another file returns Undefined 没有帮助
试试这个:
index.js:
app.get('/websites', async (req, res) => {
const View = require("./views/websitesView");
const Controller = require("./controllers/websitesController");
const Model = require("./models/websiteModel");
var controller = new Controller(Model);
var view = new View(controller);
res.send(await view.render(req, res));
})
view.js:
var controller = require("../controllers/websitesController");
class websitesView {
controller;
constructor(controller) {
this.controller = controller;
}
setTab(tab) {
this.tab = tab;
}
async render(req, res) {
res.send(`${await this.controller.getAllWebsites()}`);
}
}
module.exports = websitesView;
controller.js:
class websitesController {
model;
constructor(model) {
this.model = model;
}
async getAllWebsites() {
return this.model.find({})
}
}
module.exports = websitesController;
我正在尝试在 nodejs 中编写 MVC。问题是,渲染方法 returns 未定义,而不是数据库数据。但是如果我打电话给
Website.find({}, function(err, docs) {
res.send(`docs`);
})
直接来自 index.js - 它工作正常,我做错了什么吗?
index.js:
app.get('/websites', (req, res) => {
const View = require("./views/websitesView");
const Controller = require("./controllers/websitesController");
const Model = require("./models/websiteModel");
var model = new Model();
var controller = new Controller(model);
var view = new View(controller);
res.send(view.render(req, res));
})
view.js:
var controller = require("../controllers/websitesController");
class websitesView {
controller;
constructor(controller) {
this.controller = controller;
}
setTab(tab) {
this.tab = tab;
}
render(req, res) {
res.send(`${this.controller.getAllWebsites()}`);
}
}
module.exports = websitesView;
controller.js:
const Website = require("../models/websiteModel");
class websitesController {
model;
constructor(model) {
this.model = model;
}
getAllWebsites() {
Website.find({}, function(err, docs) {
return docs;
})
}
}
module.exports = websitesController;
model.js
const mongoose = require("mongoose")
const websiteSchema = new mongoose.Schema({
url: String,
user_id: String,
type: String,
status: Number,
timestamp: Date
}, {collection: "aaa_websites"})
module.exports = mongoose.model("Website", websiteSchema, 'aaa_websites')
Using a Mongoose model defined in another file returns Undefined 没有帮助
试试这个:
index.js:
app.get('/websites', async (req, res) => {
const View = require("./views/websitesView");
const Controller = require("./controllers/websitesController");
const Model = require("./models/websiteModel");
var controller = new Controller(Model);
var view = new View(controller);
res.send(await view.render(req, res));
})
view.js:
var controller = require("../controllers/websitesController");
class websitesView {
controller;
constructor(controller) {
this.controller = controller;
}
setTab(tab) {
this.tab = tab;
}
async render(req, res) {
res.send(`${await this.controller.getAllWebsites()}`);
}
}
module.exports = websitesView;
controller.js:
class websitesController {
model;
constructor(model) {
this.model = model;
}
async getAllWebsites() {
return this.model.find({})
}
}
module.exports = websitesController;