node.js - 未定义:即使使用 upload.single() 中间件,req.file() 也会输出未定义
问题描述
// Models
var mongoose = require('mongoose');
var ProfileSchema = new mongoose.Schema({
fullName: {
type: String,
required: true
}
// profileImage: {type: String, required: true}
});
module.exports = mongoose.model('Profile', ProfileSchema)
// Controllers
var Profile = require('../models/profile');
var multer = require('multer');
var upload = multer({dest: 'uploads/'});
exports.createProfile = (upload.single('profileImage'), function (req, res, next) {
var profileData = {
fullName: req.body.fullName,
// profileImage: req.file
}
console.log(req.file);
console.log('req.file: ', JSON.stringify(req.file));
console.log(profileData);
Profile.create(profileData, function (err, profile) {
if (err) {
// console.log(err);
res.end();
return;
// res.send(err);
}
Profile.create(function (err, profiles) {
if (err) {
res.end();
// res.send(err);
return;
}
res.json(profileData);
});
});
});
我正在尝试使用中间件在 MongoDB 数据库中同时添加文本和图像。但是,我的字段没有填充,当我尝试在控制台中打印它时,它显示 req.file(): undefined。我研究了其他问题,并指出使用“upload.single()”将解决问题。在我的情况下,它没有!第一部分是我的模型视图(架构),第二部分是我的控制器视图。
解决方案
推荐阅读
- flutter - 在另一个小部件上分离 TextSpan
- ios - 是否可以远程控制 iOS 设备上的媒体(播放/暂停/音量/...)?
- c# - Scrollviewer 滚动条不显示,需要
- excel - 从公共函数调用时 Workbook.Open 是否被禁用?
- android - 在 Android 11 中使用选择器查看 Pdf
- r - 具有二项式响应变量的 glmer 给出了奇怪的残差分布
- python - 在 jetson nano 上使用 systemd 和服务文件运行 python 脚本
- nginx - NGINX IP Hash 平衡方法未按预期工作
- mongodb - MongoDB 使用 $group 为聚合管道输出多个相同的值
- angular - Angular 单页应用程序在刷新时抛出 404 错误