node.js - 无法创建或编辑新帖子(Node 和 Mongo)
问题描述
我已经解决了这个问题,但现在它又回来了。我想将新艺术家保存到网站或编辑它们,但现在它就像应用程序没有连接到数据库但它是!我检查了。
const
express = require('express'),
router = express.Router(),
Post = require('../models/post');
// Define which ImageTypes are avaiable to upload
const imageMimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'];
// All admin Routes
router.get('/', async (req, res) => {
renderNewPage(res, new Post())
})
// New Route
router.get('/create', (req, res) => {
res.render('admin/createPost/index', {
layout: 'layouts/admin',
// Defined in models/post, gets variables
post: new Post()
});
})
// New Post create
router.post('/', async (req, res) => {
const post = new Post({
// get information from sended new Post (above) and defines variables to use in ejs files
surname: req.body.surname,
name: req.body.name,
bio: req.body.bio,
birthday: req.body.birthday,
technic: req.body.technic,
techPricerangeFrom: req.body.techPricerangeFrom,
techPricerangeTo: req.body.techPricerangeTo
})
// set FilePond saving files to DB
saveProfilpic(post, req.body.profilpic)
try { // await for new post to safe
const newPost = await post.save();
res.redirect('admin');
} catch {
renderNewPage(res, post, true)
}
})
//? Singe Artist Show, Edit, Upate, Delete
// Create single artists page
router.get('/:id', (req, res) => {
res.send('Show Artist' + req.params.id)
})
// Edit artist
router.get('/:id/edit', async (req, res) => {
try {
const findPost = await Post.findById(req.params.id)
res.render('admin/edit', {
layout: 'layouts/admin',
post: findPost
})
} catch {
res.redirect('/admin')
}
})
// Update artist text
router.put('/:id', async (req, res) => {
let post;
try {
post = await Post.findById(req.params.id)
post.surname = req.body.surname,
post.name = req.body.name,
post.bio = req.body.bio,
post.birthday = req.body.birthday,
post.technic = req.body.technic,
post.techPricerangeFrom = req.body.techPricerangeFrom,
post.techPricerangeTo = req.body.techPricerangeTo,
post.profilpic = req.body.profilpic
await post.save();
res.redirect(`/admin`);
} catch {
if (post == null) {
res.redirect('/admin')
}
else {
res.render('admin/edit', {
layout: 'layouts/admin',
post: post,
})
}
}
})
// Define functions
async function renderNewPage(res, post, hasError = false) {
// Implement all Posts (artists) in the DB
try {
const posts = await Post.find({}).collation({ locale: 'en', strength: 2 }).sort({ name: 1 }) // wait and find all post and sort my name
const params = {
layout: 'layouts/admin',
posts: posts, // take all posts from await Post.find({}) and overrides the updates the posts
}
if (hasError) params.errorMessage = 'Ein Fehler ist aufgetreten'
res.render('admin/index', params);
} catch (err) {
res.redirect('/');
}
}
// func for dave files via filepond
function saveProfilpic(post, profilpictureEncoded) {
if (profilpictureEncoded == null) return
const profpic = JSON.parse(profilpictureEncoded);
if (profpic != null && imageMimeTypes.includes(profpic.type)) { // If the file is a json obj & from the type image (jpg, png)
post.profilpic = new Buffer.from(profpic.data, 'base64') // Buffer.from(where, how)
post.profilpicType = profpic.type
}
}
module.exports = router;
模型/帖子文件中的所有类型都是字符串。如果我想更改某些内容,它会将我重定向到 /admin,这意味着它是一个错误。我得到了所有最新的包裹(快递,猫鼬,...)
解决方案
我在编写同一个教程时找到了您的答案,所以我对此仍然很陌生,但在我的情况下,问题与索引页面视图中缺少“=”有关。<% %> 部分中是否有“=”字符,以便 id 实际上包含在链接中?如果没有,链接将不包含实际的 id,因为“=”不仅需要访问变量,还需要显示它。
<% artists.forEach(Artist=> { %>
<div><%= Artist.last_name %></div>
<a href="/artists/<%= Artist.id %>">View</a>
<% }) %>
希望这会有所帮助,并随时纠正我,因为我也刚刚开始学习基础知识。
推荐阅读
- java - 我的 Android github 存储库。无法上课
- html - 使用flutter在webview中显示下载的临时本地文件(png、jpg、pdf、rtf)
- php - 动作 ProductController@store 未定义,但它确实在 Laravel 8
- c# - 检查 DevOps 管道中过时的 nuget 包
- google-cloud-platform - 云任务创建期间的身份验证范围不足错误
- html - 将切换按钮的 Html 标签转换为 erb
- c# - 是否可以在 winform c# 中不使用“IntersectWith”语法检查两个对象是否发生碰撞?
- html - 网站在移动测试中无法正确查看 没有给出错误
- javascript - 如何删除所有向左或向右的数据
- python - 如何在 Django 中仅预加载少量图像