【问题标题】:Nodejs, MongoDB upload image with multerNodejs,MongoDB用multer上传图片
【发布时间】:2019-12-10 17:08:14
【问题描述】:

我正在创建和更新配置文件路线,我能够创建和更新配置文件详细信息,但我无法将图像保存到我的数据库中。我如何将图像 url 保存到我的数据库中?

Profile Model,头像为图片:

const ProfileSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  location: {
    type: String
  },
  occupation: { type: String },
  bio: {
    type: String
  },
  date: {
    type: Date,
    default: Date.now
  },
  avatar: { type: String }
});

Multer 设置:

const storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, "./uploads/");
  },
  filename: function(req, file, cb) {
    cb(null, Date.now() + file.originalname);
  }
});
const fileFilter = (req, file, cb) => {
  if (
    file.mimetype === "image/jpeg" ||
    file.mimetype === "image/png" ||
    file.mimetype === "image/jpg"
  ) {
    cb(null, true);
  } else {
    cb(null, false);
  }
};
const upload = multer({
  storage: storage,
  limits: {
    fileSize: 1024 * 1024 * 2
  },
  fileFilter: fileFilter
});

配置文件路径,我如何将文件路径存储到我的数据库中

router.post("/", auth, upload.single("avatar"), async (req, res) => {
  console.log(req.file);

  const { location, occupation, bio } = req.body;
  const { avatar } = req.file.path;
  //Build profile object
  const profileFields = {};
  if (location) profileFields.location = location;
  if (occupation) profileFields.occupation = occupation;
  if (bio) profileFields.bio = bio;

  try {
    // Using upsert option (creates new doc if no match is found):
    let profile = await Profile.findOneAndUpdate(
      { user: req.user.id },

      { $set: profileFields },
      { new: true, upsert: true }
    );
    res.json(profile);
  } catch (err) {
    console.error(err.message);
    res.status(500).send("Server Error");
  }
});

【问题讨论】:

    标签: node.js mongodb express mongoose multer


    【解决方案1】:

    你需要像这样设置你的头像:

      if (req.file.path) profileFields.avatar = req.file.path;
    

    所以所有的代码都必须是这样的:

    router.post("/", auth, upload.single("avatar"), async (req, res) => {
      console.log(req.file.path);
    
      const { location, occupation, bio } = req.body;
      //const { avatar } = req.file.path;
      //Build profile object
      const profileFields = {};
      if (location) profileFields.location = location;
      if (occupation) profileFields.occupation = occupation;
      if (bio) profileFields.bio = bio;
      if (req.file.path) profileFields.avatar = req.file.path;
    
      try {
        // Using upsert option (creates new doc if no match is found):
        let profile = await Profile.findOneAndUpdate(
          { user: req.user.id },
    
          { $set: profileFields },
          { new: true, upsert: true }
        );
        res.json(profile);
      } catch (err) {
        console.error(err.message);
        res.status(500).send("Server Error");
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 2021-05-30
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2016-06-14
      • 2018-10-23
      • 2016-10-20
      相关资源
      最近更新 更多