Nodejs:验证失败:img:Cast to Buffer failed for value

问题描述 投票:0回答:1

我试过用猫鼬保存图像。我已遵循本教程。我的 dataSchema 如下所示,

const dataSchema = new mongoose.Schema({
  name: {
    type: String,
    required: false,
  },
  img: {
    type: Buffer,
    contentType: String,
    required: false,
  }
});

我为路由器创建了一个单独的文件夹。这是我的 routers.js 文件,

//Define storage for multer
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "uploads");
  },
  filename: (req, file, cb) => {
    const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
    cb(null, file.fieldname + "-" + uniqueSuffix);
  },
});
const upload = multer({ storage: storage });

// Post request
router.post("/", upload.single("img"), async (req, res) => {
      const data = new Data({
      name: req.body.name,
      img: {
        data: fs.readFileSync(
          path.join(__dirname, "../uploads/" + req.file.filename)
        ),
        contentType: "image/png",
      },
    });

    await Data.create(data);
    res.send(req.body);
});

我试过没有

img
场,它工作正常。但是对于图像字段,它给出了以下错误。我正在
Postman
.

中测试这个 API
(node:476) UnhandledPromiseRejectionWarning: ValidationError: Data validation failed: img: Cast to Buffer failed for value " {
  data: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 f0 00 00 01 0a 08 02 00 00 00 62 c7 90 ce 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... 22098 more bytes>,
  contentType: 'image/png'
}" (type Object) at path "img"
    at model.Document.invalidate (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\mongoose\lib\document.js:2879:32)
    at model.$set (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\mongoose\lib\document.js:1426:12)    at model.$set (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\mongoose\lib\document.js:1128:16)    at model.Document (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\mongoose\lib\document.js:148:12)
    at model.Model (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\mongoose\lib\model.js:106:12)   
    at new model (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\mongoose\lib\model.js:4752:15)    
    at C:\Users\gic\Downloads\FloodAPI\MEN API\routers\flood.js:51:17
    at Layer.handle [as handle_request] (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\gic\Downloads\FloodAPI\MEN API\node_modules\express\lib\router\route.js:137:13)    
    at Immediate.<anonymous> (C:\Users\gic\Downloads\FloodAPI\node_modules\multer\lib\make-middleware.js:53:37)
(node:476) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection 
id: 1)
(node:476) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

我以前用过

Django
。处理这些文件非常容易。 Nodejs 对我来说似乎很复杂。任何详细的解释将不胜感激。请帮我解决这个问题。

node.js mongodb express mongoose multer
1个回答
1
投票

刚刚遇到这个问题并以一种非常明显的方式解决了它。

解决它所需要做的就是在您的模式中将

type: Buffer
更改为
data: Buffer
,因为
data
字段需要Buffer类型,而不是整个
img

希望对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.