Javascript 代码工作正常,但控制台打印无法在发送到客户端后设置标题
伙计们,我创建了一个函数来限制用户上传图片,一切正常,但问题是 我在控制台上出错(发送到客户端后无法设置标题) 当我复制函数注释的整个代码时(!不是函数本身)。在控制器文件上,我没有得到错误,但是(它是相同的代码和相同的一切) 但是代码太长,所以看起来很糟糕 请在查看代码之前检查图像Javascript 代码工作正常,但控制台打印无法在发送到客户端后设置标题,javascript,node.js,express,node-modules,Javascript,Node.js,Express,Node Modules,伙计们,我创建了一个函数来限制用户上传图片,一切正常,但问题是 我在控制台上出错(发送到客户端后无法设置标题) 当我复制函数注释的整个代码时(!不是函数本身)。在控制器文件上,我没有得到错误,但是(它是相同的代码和相同的一切) 但是代码太长,所以看起来很糟糕 请在查看代码之前检查图像 const Joi = require('joi'); const appError = require('./appError') module.exports.validateHadith = (req,res
const Joi = require('joi');
const appError = require('./appError')
module.exports.validateHadith = (req,res,next)=>{
const hadithSchema = Joi.object({
Hadith: Joi.object({
narrator: Joi.string().required(),
description:Joi.string().required().min(15),
hadith:Joi.string().required().min(15),
}).required()
})
const {error} = hadithSchema.validate(req.body);
if(error){
const msg = error.details.map(el=>el.message).join(',')
throw new appError(msg,400)
} else{
next()
}
};
module.exports.reviewValidation = (req,res,next) => {
const reviewJoiSchema = Joi.object({
review: Joi.object({
comment: Joi.string().required(),
rating: Joi.number().required(),
}).required()
})
const {error} = reviewJoiSchema.validate(req.body);
if(error) {
const msg = error.details.map(el=>el.message).join(',')
throw new appError(msg,400)
} else {
next()
}
}
module.exports.imageValidation = (req,res,next) => {
const imageSchema = Joi.array().min(1).max(6).required()
const {error} = imageSchema.validate(req.files)
if(error) {
const msg = error.details.map(el=>el.message).join(',')
throw new appError(msg,400)
} else{
next()
}
}
module.exports.imageEditValidation = async (updatedHadith,images,req,res,id) => {
const length = updatedHadith.images.length
const max = 6
if(images.length+length <= max) {
updatedHadith.images.push(...images)
await updatedHadith.save()
console.log(updatedHadith)
} else{
req.flash('error','Sorry you can`t have more than 6 images')
return res.redirect(`/hadith/${id}`)
}
}
const Joi=require('Joi');
const appError=require(“./appError”)
module.exports.validateHadith=(请求、回复、下一步)=>{
const hadithSchema=Joi.object({
圣训:Joi.object({
讲述人:Joi.string().required(),
描述:Joi.string().required().min(15),
圣训:Joi.string().required().min(15),
}).required()
})
const{error}=hadithSchema.validate(req.body);
如果(错误){
const msg=error.details.map(el=>el.message.join(','))
抛出新外观(味精,400)
}否则{
下一个()
}
};
module.exports.reviewValidation=(请求、回复、下一步)=>{
const reviewJoiSchema=Joi.object({
复习:Joi.object({
注释:Joi.string().required(),
额定值:Joi.number().required(),
}).required()
})
const{error}=reviewJoiSchema.validate(req.body);
如果(错误){
const msg=error.details.map(el=>el.message.join(','))
抛出新外观(味精,400)
}否则{
下一个()
}
}
module.exports.imageValidation=(请求、恢复、下一步)=>{
常量imageSchema=Joi.array().min(1.max(6.required)()
const{error}=imageSchema.validate(req.files)
如果(错误){
const msg=error.details.map(el=>el.message.join(','))
抛出新外观(味精,400)
}否则{
下一个()
}
}
module.exports.imageEditValidation=async(updatedHadith、images、req、res、id)=>{
const length=updatedHadith.images.length
常数max=6
如果(images.length+length您需要添加wait
等待imageEditValidation(updateHadith、images、req、res、id)
目前,它没有等待,因此它会跳转到req.flash(“成功”,“成功编辑圣训”)
,而不等待它返回
您还需要在`console.log(updatedHadith)之后添加一个return
还需要删除req.flash和res.redirect('/hadith/${id}')语句,并将其替换为return
验证程序代码将被删除
module.exports.imageEditValidation = async (updatedHadith,images,req,) => {
const length = updatedHadith.images.length
const max = 6
if(images.length+length <= max) {
updatedHadith.images.push(...images)
await updatedHadith.save()
console.log(updatedHadith)
return true
} else{
req.flash('error','Sorry you can`t have more than 6 images')
return false
}
}
module.exports.postEditForm =async (req, res, next) => {
const {id} = req.params;
const updatedHadith = await HadithModel.findByIdAndUpdate(
id,
{...req.body.Hadith},
{ runValidators: true }
);
const images = req.files.map(f => ({url : f.path ,filename: f.filename}))
if(await imageEditValidation (updatedHadith,images,req)){
req.flash("success", " The Hadith is successfully Edited :)");
}
res.redirect(`/hadith/${id}`);
}如果验证失败,您将运行两次res.redirect
:首先在postEditForm
第61行,然后在imageEditValidation
第57行异步运行。因此,您试图在返回成功后返回错误。请将代码以文本形式粘贴到问题中,并将其格式化为代码。这样人们可以将其复制并修改为答案,并为其编制索引以进行搜索。图像中的代码不是stackoverflow的方式。请将代码作为文本而不是图像插入问题。是的,但即使这样,它也无法工作,因为成功代码不是有条件的,即使错误代码已经运行,它也会运行(通过等待,它只是颠倒了这些事情发生的顺序)-还需要另一个步骤,在验证失败时不运行成功代码out@CherryDT你说得对