Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

349
Visualizações
Multer Error: Image url is not save in my Database

At first i met Multer: Unexpected field error, after i fixed until here. every boards column is save in DB except image. is my postman test method is wrong? or my code have problem? how can i save image url(path) in boards image column?

const upload = multer({
  storage: multer.diskStorage({
    destination(req, file, done) {
      done(null, "uploads/");
    },
    filename(req, file, done) {
      const ext = path.extname(file.originalname);
      const basename = path.basename(file.originalname, ext);
      done(null, basename + new Date().getTime() + ext);
    },
  }),
  limits: { fileSize: 5 * 1024 * 1024 }, // 5MB
});

router.post("/images", upload.single("image"), (req, res, next) => {
  res.json({ url: `/uploads/${req.file.filename}` });
});

router.post("/", upload.single("image"), async (req, res, next) => {
  try {
    const {
      category,
      title,
      description,
      tags,
      latitude,
      longitude,
      mainAddress,
      detailAddress,
    } = req.body;
    const createBoards = await boards.create({
      category,
      title,
      description,
      tags,
      latitude,
      longitude,
      mainAddress,
      detailAddress,
      image: req.body.url,
    });
    return res.status(200).json({ data: createBoards, message: "OK" });
  } catch (err) {
    return res.status(500).json({ message: "Error"});
  }
});

enter image description here

enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If by url you mean the actual path multer has assigned for your uploaded file, you can use the property path on the file object. See this for all available properties.

So in your case you can do:

const createBoards = await boards.create({
      category,
      title,
      description,
      tags,
      latitude,
      longitude,
      mainAddress,
      detailAddress,
      image: req.file.path // to be safe you should maybe add a check if req.file is defined
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda