Multer图片上载错误:意外令牌-JSON在位置0,语法错误:意外令牌#在JSON中位置0 [英] Error in Multer image upload: Unexpected token - in JSON at position 0, SyntaxError: Unexpected token # in JSON at position 0

查看:122
本文介绍了Multer图片上载错误:意外令牌-JSON在位置0,语法错误:意外令牌#在JSON中位置0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我摆脱困境.这是我的uploadRouter.js,其中我尝试使用multer模块从POSTMAN上传图像文件

Please help me to get out of this. This is my uploadRouter.js in which I am trying to upload image file from POSTMAN using multer module

const express = require('express');
const mongoose = require('mongoose');
const autheticate = require('../authenticate');
const multer = require('multer')
const bodyParser = require('body-parser');
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'public/images');
    },
    filename: (req, file, cb) => {
        cb(null, file.originalname);
    }
});
const imageFileFilter = (req, file, cb) => {
    if(!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
        return cb(new Error('You can upload only image files!'), false);
    }
    cb(null, true);
};
const upload = multer({ storage: storage, fileFilter: imageFileFilter});
//const uploadImgFile = multer().single('imageFile');
const uploadRouter=express.Router();
uploadRouter.use(bodyParser.json());
uploadRouter.route('/')
.get(autheticate.verifyUser,autheticate.verifyAdmin,(req,res,next)=>{
    res.statusCode = 403;
    res.end('GET operation not supported on /imageUpload');
})
.post(autheticate.verifyUser,autheticate.verifyAdmin,upload.single('imageFile'),(req,res,next)=>{
   res.statusCode=200;
   res.setHeader('Content-Type','application/json');
   res.end('End');
})
.put(autheticate.verifyUser,autheticate.verifyAdmin,(req,res,next)=>{
    res.statusCode = 403;
    res.end('GET operation not supported on /imageUpload');
})
.delete(autheticate.verifyUser,autheticate.verifyAdmin,(req,res,next)=>{
    res.statusCode = 403;
    res.end('GET operation not supported on /imageUpload');
})
module.exports = uploadRouter;

我已将请求消息的正文类型从POSTMAN设置为form-data
但是当我上传图片时更改了正文格式后,POSTMAN中给出了以下错误

I have set the body type of request message to form-data from POSTMAN,
But after changing body-format when I am uploading the image, the following error is given in POSTMAN

    <body>
        <h1>Unexpected token - in JSON at position 0</h1>
        <h2>400</h2>
        <pre>SyntaxError: Unexpected token # in JSON at position 0
    at JSON.parse (&lt;anonymous&gt;)
    at createStrictSyntaxError (F:\Cousera\Node\coursera-node-confusion-server\node_modules\body-parser\lib\types\json.js:157:10)
    at parse (F:\Cousera\Node\coursera-node-confusion-server\node_modules\body-parser\lib\types\json.js:83:15)
    at F:\Cousera\Node\coursera-node-confusion-server\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (F:\Cousera\Node\coursera-node-confusion-server\node_modules\raw-body\index.js:224:16)
    at done (F:\Cousera\Node\coursera-node-confusion-server\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (F:\Cousera\Node\coursera-node-confusion-server\node_modules\raw-body\index.js:273:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1055:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)</pre>
    </body>
</html>

POSTMAN中包含两个标头:
1.内容类型:"application/json"
2.身份验证:承载[[TOKEN]]
请我无法理解该错误,我是node.js的新手.请帮我解决它

Two headers are contained in POSTMAN:
1. Content-Type: 'application/json'
2. Authentication: bearer [[TOKEN]]
Please I am unable to understand the error, I am very new to node.js. Please help me through it

推荐答案

当您将Content-Type作为application/json传递并上传其content-type不是json的文件时,会发生此问题. 删除此标头,因为它不是必需的.

This issue occurs as you are passing Content-Type as application/json and uploading a file whose content-type is not json. Remove this header as it is not required.

这篇关于Multer图片上载错误:意外令牌-JSON在位置0,语法错误:意外令牌#在JSON中位置0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆