我创建了这个nodejs应用程序,让同学们发布他们想卖的东西的广告,我把它托管在heroku上,但是当一个用户登录并试图创建一个广告时,他会被重定向到他们的仪表板上(就像它的编程做的那样),但是没有显示他们创建的任何广告,即使是在刷新之后。同时,在mongodb上,他们也没有创建广告对象。这个应用程序不会崩溃或停止工作。我真的是一个初学者,但是任何帮助都会很感激。
下面是用于数据输入的handlebarsjs模板
<header id="details-header">
<h5><a href="/">Go back to Home</a></h5>
<p></p>
</header>
<div class="registration-form">
<form class="form custom__form" enctype="multipart/form-data">
<div class="form-icon">
<span><i class="fa-light fa-money-check-pen"></i></span>
</div>
<div >
<p>Add image</p>
<div class="custom__image-container">
<label id="add-img-label" for="add-single-img">+</label>
<input type="file" id="add-single-img" accept="image/jpeg" />
</div>
<input type="file" class="item form-control" id="image-input" name="profile-files" accept="image/jpeg" multiple />
</div>
<div class="form-group">
<input type="text" class="form-control item" name="title" id="title" placeholder="Title" required>
</div>
<div class="form-group">
<textarea type="text" id="body" name="body" class="form-control item" autocomplete="off" placeholder="Enter the product description"></textarea>
</div>
<div class="form-group">
<select name="category" id="category" class=" form-control item">
<option value="">Select a category :</option>
<option value="beds and mattresses">Beds & mattresses</option>
<option value="chairs and tables">Chairs & Tables</option>
<option value="laptops and phones">Laptops & Phones</option>
<option value="appliances and electronics">Appliances & electronics</option>
<option value="clothing">Clothing</option>
<option value="services and foods">Services & Foods</option>
<option value="medical equipment">Medical Equipment</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control item" name="price" id="price" placeholder="Price e.g 20000" required>
</div>
<div class="form-group">
<input type="text" class="form-control item" name="location" id="location" placeholder="Location" required>
</div>
<div class="form-group">
<input type="text" class="form-control item" name="phone-number" id="phone-number" placeholder="Phone Number e.g 0754536336" required>
</div>
<div class="form-group">
<input type="text" class="form-control item" name="displayname" id="displayname" placeholder="Your name users will see" required>
</div>
<div class="form-group">
<input type="submit" value="Post Ad" class="btn btn-block create-account">
</div>
</form>下面是运行在窗体上的函数的form.js
const title = document.getElementById('title')
const body = document.getElementById('body')
const price = document.getElementById('price')
const locationText = document.getElementById('location')
const phone = document.getElementById('phone-number')
const category = document.getElementById('category')
const displayname = document.getElementById('displayname')
const imgInputHelper = document.getElementById("add-single-img");
const imgInputHelperLabel = document.getElementById("add-img-label");
const imgContainer = document.querySelector(".custom__image-container");
const imgFiles = [];
const addImgHandler = () => {
const file = imgInputHelper.files[0];
if (!file) return;
// Generate img preview
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
const newImg = document.createElement("img");
newImg.src = reader.result;
imgContainer.insertBefore(newImg, imgInputHelperLabel);
};
// Store img file
imgFiles.push(file);
// Reset image input
imgInputHelper.value = "";
return;
};
imgInputHelper.addEventListener("change", addImgHandler);
const getImgFileList = (imgFiles) => {
const imgFilesHelper = new DataTransfer();
imgFiles.forEach((imgFile) => imgFilesHelper.items.add(imgFile));
return imgFilesHelper.files;
};
const customFormSubmitHandler = (ev) => {
ev.preventDefault();
const firstImgInput = document.getElementById("image-input");
firstImgInput.files = getImgFileList(imgFiles);
// submit form to server, etc
fileUpload(firstImgInput.files);
window.location.href='/dashboard'
};
document
.querySelector(".custom__form")
.addEventListener("submit", customFormSubmitHandler);
// Function that handles file upload using XHR
const fileUpload = (files) => {
// Create FormData instance
const fd = new FormData();
// Iterate over all selected files
Array.from(files).forEach(file => {
fd.append('product-file', file);
});
fd.append('title', title.value)
fd.append('body', body.value)
fd.append('price', price.value)
fd.append('location', locationText.value)
fd.append('number', phone.value)
fd.append('category', category.value)
fd.append('displayname', displayname.value)
// Create XHR rquest
const xhr = new XMLHttpRequest();
// Log HTTP response
xhr.onload = () => {
// console.log(xhr.response);
console.log('success')
};
// Send XHR reqeust
xhr.open('POST', `/products`);
xhr.send(fd);
};下面是将数据保存到数据库的nodejs路由代码
//@desc Process add form
//@route POST /products
router.post('/products', ensureAuthenticated, upload.array("product-file"), async (req, res) => {
// var locaFilePath = req.file.path;
// var result = await uploadToCloudinary(locaFilePath);
// console.log(result)
// console.log(req.file)
var imageUrlList = [];
for (var i = 0; i < req.files.length; i++) {
var locaFilePath = req.files[i].path;
var result = await uploadToCloudinary(locaFilePath);
imageUrlList.push(result.url);
}
try {
let product = new Product({
title: req.body.title,
body: req.body.body,
image1: imageUrlList[0],
image2: imageUrlList[1],
image3: imageUrlList[2],
price: req.body.price,
category: req.body.category,
phone_number : convertNumberToInternational(req.body.number.trim()),
location: req.body.location,
name: req.body.displayname,
user: req.user.id,
})
await product.save()
// req.body.user = req.user.id
// await Product.create(req.body)
res.redirect('/dashboard')
} catch (err) {
console.error(err)
res.render('error/500')
}
})虽然排得太多,但请帮帮我。
发布于 2022-10-19 13:54:27
你好,朋友,从您的代码我可以看到您创建新的对象,添加到您的数据库中使用nodejs,然后您重定向到另一个页面(仪表板),即correct.When您发布了一些新的东西,您的仪表板页面应该动态更改和显示新的contents.Thats为什么您应该使用ejs在您的项目,创建动态页,这样您的仪表板页面将显示每一个新的广告将被张贴。
从创建视图目录和内部创建dashboard.ejs文件开始,内部显示来自users.Something的新广告(ads数组),如下所示
//add your title and anything else you want in your page
<% ads_array.forEach(function(ad){ %>
<h1><%=ad.title%></h1>
<p>
<% }) %>在nodejs的post请求中,将用户重定向到dashboard.ejs文件并将新的广告推送到ads数组
res.render("dashboard", {
ads_array: ads
}我可以简单地说,您需要创建dashboard.ejs文件(./view/dashboard.ejs),这将是一个动态页面,它将显示用户发布的广告,这是在每一篇新帖子中更改动态页面的方式。
在nodejs文件中,您在此页面中发出post请求并重定向用户,以查看您还需要创建一个名为ads的新数组,在该数组中,您将在post request.This信息(ads数组)中创建的每一个新广告推送到dashboard.ejs文件中,以便在仪表板页面中显示它。
您正在尝试将动态内容添加到静态页面中,这就是为什么您必须使用ejs模板。我希望这将帮助您看到错误,对于heroku问题,请确保您遵循heroku文档中的所有步骤。
https://stackoverflow.com/questions/74121960
复制相似问题