TypeError:Organizations.find 不是 C:\Users\dillo\Desktop\UWI\ECNG 3020 Final Year Project\Damage_Assessment _Tool\routers\OrganizationsRouters.js:11:23 中的函式
我似乎無法理解有什么問題。為簡單起見,我將路由分成不同的檔案,但我一直遇到此錯誤“TypeError:Organizations.find is not a function”
路由檔案包含需要的路由: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}
uj5u.com熱心網友回復:
我猜這兩行有問題
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)
}
}
)
})
uj5u.com熱心網友回復:
將以書面形式運行我的除錯..
TypeError: Organizations.find is not a function
那么什么是組織...
const Organizations = require('../db/models/Organizations.model')
它標記為模型...您需要創建它的實體嗎?或者使用 ORM 工具進行查找而不是在模型上呼叫 find?什么是組織。
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);
...好的,所以我們有貓鼬...我以前沒有使用過它,假設它是一個 orm 或某個包而不是函式...它是...所以我們需要/有一個連接嗎?我認為不是——所以也許我們需要建立一個聯系?
https://mongoosejs.com/docs/connections.html
所以你的詳細資訊是這樣的。
mongoose.connect('mongodb://localhost:27017/myapp');
uj5u.com熱心網友回復:
嘗試將模型的匯入更改OrganizationsRouter.js為:
const { Organizations } = require('../db/models/Organizations.model')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406252.html
標籤:
