更新路由不适用于合并的快速路由器参数
Update route not working on merged express router params
我正在学习 MERN 堆栈并在我的子路由器的编辑路由上遇到问题。
我在 songs.js
和 students.js
文件中有以下模型模式:
const mongoose = require('mongoose');
const studentSchema = mongoose.Schema({
name: { type: String, required: true },
instrument: String,
songs: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Song'
}]
});
const Student = mongoose.model('Student', studentSchema);
module.exports = Student;
const mongoose = require('mongoose');
const songSchema = mongoose.Schema({
name: { type: String, required: true },
img: String
})
const Song = mongoose.model('Song', songSchema);
module.exports = Song
我的路由器有 songs.js
和 students.js
文件,我的歌曲路由器 const songs = express.Router({ mergeParams: true });
的 mergeParams 设置为 true。我像这样将它连接到学生路由器:
students.use('/:id/songs', songs);
例如,我的 url 参数变为 students/student1/songs/song1
我的所有其他路由都正常工作,但是在我的歌曲路由器的更新路由上,当我重定向回歌曲的索引视图时出现错误 "TypeError: Student.findById is not a function"。我的编辑和更新路线
如下:
songs.get('/:songId/edit', async (req, res) => {
try {
const findSong = Song.findById(req.params.songId);
const findStudent = Student.findById = (req.params.id);
const [foundSong, foundStudent] = await Promise.all([findSong, findStudent]);
res.render('songs/edit', {
student: foundStudent,
song: foundSong
})
} catch (err) {
console.log(err);
}
});
songs.put('/:songId', async (req, res) => {
try {
const updateSong = await Song.findByIdAndUpdate(req.params.songId, req.body, { new: true });
res.redirect('/students/' + req.params.id + '/songs');
} catch (err) {
console.log(err);
}
});
我不确定是什么导致了这里的错误,我的删除路由设置类似并且正在运行。将不胜感激任何建议。
你的死记硬背 (req.params.id) 没有定义。
songs.get('/:songId/edit', async (req, res) => {
try {
const findSong = Song.findById(req.params.songId);
**const findStudent = Student.findById = (req.params.id);**
const [foundSong, foundStudent] = await Promise.all([findSong, findStudent]);
res.render('songs/edit', {
student: foundStudent,
song: foundSong
})
} catch (err) {
console.log(err);
}
});
我正在学习 MERN 堆栈并在我的子路由器的编辑路由上遇到问题。
我在 songs.js
和 students.js
文件中有以下模型模式:
const mongoose = require('mongoose');
const studentSchema = mongoose.Schema({
name: { type: String, required: true },
instrument: String,
songs: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Song'
}]
});
const Student = mongoose.model('Student', studentSchema);
module.exports = Student;
const mongoose = require('mongoose');
const songSchema = mongoose.Schema({
name: { type: String, required: true },
img: String
})
const Song = mongoose.model('Song', songSchema);
module.exports = Song
我的路由器有 songs.js
和 students.js
文件,我的歌曲路由器 const songs = express.Router({ mergeParams: true });
的 mergeParams 设置为 true。我像这样将它连接到学生路由器:
students.use('/:id/songs', songs);
例如,我的 url 参数变为 students/student1/songs/song1
我的所有其他路由都正常工作,但是在我的歌曲路由器的更新路由上,当我重定向回歌曲的索引视图时出现错误 "TypeError: Student.findById is not a function"。我的编辑和更新路线 如下:
songs.get('/:songId/edit', async (req, res) => {
try {
const findSong = Song.findById(req.params.songId);
const findStudent = Student.findById = (req.params.id);
const [foundSong, foundStudent] = await Promise.all([findSong, findStudent]);
res.render('songs/edit', {
student: foundStudent,
song: foundSong
})
} catch (err) {
console.log(err);
}
});
songs.put('/:songId', async (req, res) => {
try {
const updateSong = await Song.findByIdAndUpdate(req.params.songId, req.body, { new: true });
res.redirect('/students/' + req.params.id + '/songs');
} catch (err) {
console.log(err);
}
});
我不确定是什么导致了这里的错误,我的删除路由设置类似并且正在运行。将不胜感激任何建议。
你的死记硬背 (req.params.id) 没有定义。
songs.get('/:songId/edit', async (req, res) => {
try {
const findSong = Song.findById(req.params.songId);
**const findStudent = Student.findById = (req.params.id);**
const [foundSong, foundStudent] = await Promise.all([findSong, findStudent]);
res.render('songs/edit', {
student: foundStudent,
song: foundSong
})
} catch (err) {
console.log(err);
}
});