我尝试在node.js上创建我的API服务器并获取此错误
{
"error": {
"message": "ENOENT: no such file or directory, open 'X:\\projects\\Brand-server\\uploads\\2018-06-08T07:45:55.176Zllg.png'"
}
}
在我尝试在服务器中为产品保存图像之后发生了它但它不起作用。我使用了multer和Postman(用于测试API)。 这是我的代码:
const express = require('express')
const router = express.Router()
const mongoose = require('mongoose')
const multer = require('multer')
const storage = multer.diskStorage({
destination: function (req,file,cb) {
cb(null,'./uploads/')
},
filename: function(req,file,cb){
cb(null, new Date().toISOString() + file.originalname)
}
})
const upload = multer({storage:storage})
const Product = require('../models/products')
router.post('/',upload.single('productImage'), (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
})
product.save()
.then((result) => {
console.log(result)
res.status(201).json({
createdProduct: result
})
})
.catch(err => {
console.log(err)
res.status(500).json({
error: err
})
})
})
答案 0 :(得分:1)
我解决了这个问题!) 存储问题。
const storage = multer.diskStorage({
destination: function (req,file,cb) {
cb(null,'./uploads/')
},
filename: function(req,file,cb){
cb(null,Date.now() + '-' + file.originalname)
}
})