Express 添加mongoose模式模型以表示错误500中的结果
我有一个react应用程序,它通过axios将数据存储到mongoose服务器。在我想为不同的数据添加一个额外的模式之前,它工作得非常好。我的模式模型是分开的,所以我想只添加另一个名为PartyList.jsExpress 添加mongoose模式模型以表示错误500中的结果,express,mongoose,Express,Mongoose,我有一个react应用程序,它通过axios将数据存储到mongoose服务器。在我想为不同的数据添加一个额外的模式之前,它工作得非常好。我的模式模型是分开的,所以我想只添加另一个名为PartyList.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; // Define collection and schema for Items var PartyList = new Schema({ name:
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)。运行使用此路由的部分应用程序后,我得到一个500(内部服务器错误)。我的服务器告诉我ReferenceError:PartyList没有定义,这是上面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 partyList=新partyList(请求正文)
然后使用partyList
作为变量您有两个相同名称的常量。重命名管线中的PartyList