express - 添加猫鼬模式模型来表达导致错误500
问题描述
我有通过 axios 将数据存储到猫鼬服务器的反应应用程序。在我想为不同的数据添加额外的模式之前,它工作得很好。我的模式模型是分开的,所以我想只添加另一个名为PartyList.js的模型。
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Define collection and schema for Items
var PartyList = new Schema({
name: {
type: String
},
song_id: {
type: String
},
port: {
type: Number
}
},{
collection: 'party'
});
module.exports = mongoose.model('PartyList', PartyList);
这是我的 app.js 中的代码。
const config = require('./database/DB');
const ServerPortRouter = require('./routes/ServerPortRoutes');
mongoose.connect(config.DB).then(
() => {console.log('Database is connected') },
err => { console.log('Can not connect to the database' +err)
});
app.use('/serverport', ServerPortRouter);
这就是我导入它并尝试运行它的方式(称为 ServerPortRoutes.js)。在运行使用此路由的应用程序的一部分后,我得到一个 50 0 (Internal Server Error)。我的服务器告诉我ReferenceError: PartyList is not defined上面定义了 3 行。
const ServerPort = require('../models/ServerPort');
const PartyList = require('../models/PartyList');
ServerPortRouter.route('/add-party').post(function (req, res) {
const PartyList = new PartyList(req.body);
PartyList.save()
.then(PartyList => {
res.json('Server added successfully');
})
.catch(err => {
res.status(500).send("unable to save to database");
});
});
解决方案
问题似乎是您正在重新定义 const 。在您的路线更改为const partyList = new PartyList(req.body);
然后partyList
用作您的变量
推荐阅读
- .net - JsonIgnore 仅在发送 GET 请求时
- r - 如何在R中绘制具有移动阈值的百分比
- sorting - 对多个文件中的重复项进行排序
- docker - 在 docker 容器中执行的 Java 应用程序中运行 docker 命令
- java - 按对象的值对linkedList进行排序
- javascript - 在本地调用 Firebase 云函数时,我收到 200 响应,但响应是 cors 类型响应,而不是我的实际数据
- python - 是否有一种简洁的方法可以删除 GroupBy 对象的每一组中的行?
- r - 我不能在 R 中使用 ggmap 或任何重新电镀的资源来绘制我的地图
- serialization - 在 ASP.NET Core API 服务中允许无效日期
- windows - Windows 上的 LinkOnceODRLinkage 用于全局变量