node.js - Why multer.diskStorage() is not called? - Stack Overflow

Newcomer to nodejs.I write the code below and use thunder client to test, finding an error: 500.Image

Newcomer to nodejs.

I write the code below and use thunder client to test, finding an error: 500.

Image storage engine
const storage = multer.diskStorage({
  destination: './upload/images',
  filename: (req, file, cb)=>{
    return cb(null, `${file.fieldname}_${Date.now()}${path.extname(file.originalname)}`)
  }
})
const upload = multer({storage: storage})
// Create upload endpoint for images
app.use('/images', express.static('upload/images'))
app.post("/upload", upload.single('product'), (req, res)=>{
  res.json({
      success: 1,
      image_url: `http://localhost:${port}/images/${req.file.filename}`
  })
})

This is the error information.

TypeError: Cannot read properties of undefined (reading 'filename')
    at /home/pp/Desktop/E-Commerce/backend/index.js:37:62
    at Layer.handle [as handle_request] (/home/pp/Desktop/E-Commerce/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/pp/Desktop/E-Commerce/backend/node_modules/express/lib/router/route.js:149:13)
    at Immediate._onImmediate (/home/pp/Desktop/E-Commerce/backend/node_modules/multer/lib/make-middleware.js:53:37)
    at process.processImmediate (node:internal/timers:478:21)

If I remove image_url, the test can be passed. I am sure other part of the code is correct, because if I remove the code above, everything goes fine.

But actually I am wondering if multer is actually working, because when I add an output in multer.diskStorage() and execute the code below, there is no "hello" on the console. So I think multer.diskStorage() function is not called at all.

Image storage engine
const storage = multer.diskStorage({
  destination: './upload/images',
  filename: (req, file, cb)=>{
    console.log("hello")
    return cb(null, `${file.fieldname}_${Date.now()}${path.extname(file.originalname)}`)
  }
})
const upload = multer({storage: storage})
// Create upload endpoint for images
app.use('/images', express.static('upload/images'))
app.post("/upload", upload.single('product'), (req, res)=>{
  res.json({
      success: 1,
  })
})

Anyone met such problem before? Thanks a lot!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632685a4637202.html

相关推荐

  • node.js - Why multer.diskStorage() is not called? - Stack Overflow

    Newcomer to nodejs.I write the code below and use thunder client to test, finding an error: 500.Image

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信