TypeError: Organizations.find is not a function
TypeError: Organizations.find is not a function
TypeError: Organizations.find 不是函数
在 C:\Users\dillo\Desktop\UWI\ECNG 3020 最后一年 Project\Damage_Assessment _Tool\routers\OrganizationsRouters.js:11:23
我似乎看不出有什么问题。为了简单起见,我将我的路线分成不同的文件,但我一直遇到这个错误“TypeError:Organizations.find 不是一个函数”
路由文件包含所需的路由:
OrganizationsRouter.js
const express = require('express')
const router = new express.Router();
const { mongoose } = require ('../db/mongoose');
const Organizations = require('../db/models/Organizations.model')
//Routes for Organizations
router.get('/Organizations', (req,res) => {
//return an array of all the damage assessments made that is stored on the database.
Organizations.find({}).then((organization) => {
res.send(organization);
}).catch((e) => {
res.send(e);
});
})
router.post('/Organizations', (req,res) => {
//create a damage assessment report and save to the database
let organizationName = req.body.organizationName;
let newOrganization = new Organizations({
organizationName
});
newOrganization.save().then((OrganizationDoc) => {
//the full Organization document is returned (including id)
res.send(OrganizationDoc);
})
});
router.patch('/Organizations/:id', (req,res) => {
//update the Organization specified
Organizations.findOneAndUpdate({_id: req.params.id},{
$set: req.body
}).then(() => {
res.send({'message' : "Updated Successfully"});
});
});
router.delete('/Organizations/:id', (req,res) => {
//delete the Organization specified
Organizations.findOneAndRemove({
_id: req.params.id
}).then((removeOrganizationDoc) => {
res.send(removeOrganizationDoc);
})
});
module.exports = router
App.js 是主索引文件:
App.js
const express = require('express');
const app = express();
const { mongoose } = require ('./db/mongoose');
const bodyParser = require ('body-parser');
const res = require('express/lib/response');
/*Load Middleware*/
app.use(bodyParser.json());
//Import Routers
const organizationRouter = require('./routers/OrganizationsRouters')
//Register Router
app.use(organizationRouter)
//Listening to the server on port 3000
app.listen(3000,()=>{
console.log("Listening to port 3000");
})
Organizations.model.js 是猫鼬模式:
Organizations.model.js
const mongoose = require('mongoose');
const OrganizationsSchema = new mongoose.Schema({
organizationName:{
type: String,
required: true,
minlength:1,
trim: true
}
})
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
module.exports = {Organizations}
估计这两行有问题
const { mongoose } = require ('../db/mongoose');
const Organizations = require('../db/models/Organizations.model')
在某些时候应该有 mongoose.createConnection 调用以及 connection.model('Organisations', '../db/models/Organizations.model')
然后
router.get('/Organizations', (req,res) => {
Organizations.find(
{"name": "blah"},
(err, docs) => {
if(err) {
res.send(err)
} else {
res.send(docs)
}
}
)
})
去运行我的书面调试..
TypeError: Organizations.find is not a function
那么什么是组织...
const Organizations = require('../db/models/Organizations.model')
它被标记为模型...您需要创建它的实例吗?还是使用 ORM 工具进行查找而不是在模型上调用查找?什么是组织。
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
...好吧,我们有猫鼬...我以前没用过它,假设它是一个 orm 或一些包而不是功能...它是...所以我们需要/有一个连接到吗?我认为不是 - 所以也许我们需要创建一个连接?
https://mongoosejs.com/docs/connections.html
你的详细信息是这样的。
mongoose.connect('mongodb://localhost:27017/myapp');
尝试将OrganizationsRouter.js
中模型的导入改成:
const { Organizations } = require('../db/models/Organizations.model')
所以我想通了问题。
const mongoose = require('mongoose');
const OrganizationsSchema = new mongoose.Schema({
organizationName:{
type: String,
required: true,
minlength:1,
trim: true
}
})
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
module.exports = Organizations;
通过删除“组织”猫鼬模式周围的括号,它工作正常。
TypeError: Organizations.find 不是函数 在 C:\Users\dillo\Desktop\UWI\ECNG 3020 最后一年 Project\Damage_Assessment _Tool\routers\OrganizationsRouters.js:11:23
我似乎看不出有什么问题。为了简单起见,我将我的路线分成不同的文件,但我一直遇到这个错误“TypeError:Organizations.find 不是一个函数”
路由文件包含所需的路由: OrganizationsRouter.js
const express = require('express')
const router = new express.Router();
const { mongoose } = require ('../db/mongoose');
const Organizations = require('../db/models/Organizations.model')
//Routes for Organizations
router.get('/Organizations', (req,res) => {
//return an array of all the damage assessments made that is stored on the database.
Organizations.find({}).then((organization) => {
res.send(organization);
}).catch((e) => {
res.send(e);
});
})
router.post('/Organizations', (req,res) => {
//create a damage assessment report and save to the database
let organizationName = req.body.organizationName;
let newOrganization = new Organizations({
organizationName
});
newOrganization.save().then((OrganizationDoc) => {
//the full Organization document is returned (including id)
res.send(OrganizationDoc);
})
});
router.patch('/Organizations/:id', (req,res) => {
//update the Organization specified
Organizations.findOneAndUpdate({_id: req.params.id},{
$set: req.body
}).then(() => {
res.send({'message' : "Updated Successfully"});
});
});
router.delete('/Organizations/:id', (req,res) => {
//delete the Organization specified
Organizations.findOneAndRemove({
_id: req.params.id
}).then((removeOrganizationDoc) => {
res.send(removeOrganizationDoc);
})
});
module.exports = router
App.js 是主索引文件: App.js
const express = require('express');
const app = express();
const { mongoose } = require ('./db/mongoose');
const bodyParser = require ('body-parser');
const res = require('express/lib/response');
/*Load Middleware*/
app.use(bodyParser.json());
//Import Routers
const organizationRouter = require('./routers/OrganizationsRouters')
//Register Router
app.use(organizationRouter)
//Listening to the server on port 3000
app.listen(3000,()=>{
console.log("Listening to port 3000");
})
Organizations.model.js 是猫鼬模式: Organizations.model.js
const mongoose = require('mongoose');
const OrganizationsSchema = new mongoose.Schema({
organizationName:{
type: String,
required: true,
minlength:1,
trim: true
}
})
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
module.exports = {Organizations}
估计这两行有问题
const { mongoose } = require ('../db/mongoose');
const Organizations = require('../db/models/Organizations.model')
在某些时候应该有 mongoose.createConnection 调用以及 connection.model('Organisations', '../db/models/Organizations.model') 然后
router.get('/Organizations', (req,res) => {
Organizations.find(
{"name": "blah"},
(err, docs) => {
if(err) {
res.send(err)
} else {
res.send(docs)
}
}
)
})
去运行我的书面调试..
TypeError: Organizations.find is not a function
那么什么是组织...
const Organizations = require('../db/models/Organizations.model')
它被标记为模型...您需要创建它的实例吗?还是使用 ORM 工具进行查找而不是在模型上调用查找?什么是组织。
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
...好吧,我们有猫鼬...我以前没用过它,假设它是一个 orm 或一些包而不是功能...它是...所以我们需要/有一个连接到吗?我认为不是 - 所以也许我们需要创建一个连接?
https://mongoosejs.com/docs/connections.html
你的详细信息是这样的。
mongoose.connect('mongodb://localhost:27017/myapp');
尝试将OrganizationsRouter.js
中模型的导入改成:
const { Organizations } = require('../db/models/Organizations.model')
所以我想通了问题。
const mongoose = require('mongoose');
const OrganizationsSchema = new mongoose.Schema({
organizationName:{
type: String,
required: true,
minlength:1,
trim: true
}
})
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
module.exports = Organizations;
通过删除“组织”猫鼬模式周围的括号,它工作正常。