1

It says me syntax error: JSON Parse error. unrecognized token '<'

Iam using Fetch to do the request.It let me send short base64 strings i tried so what can i do?

This is my call to the api:

export function uploadPost(post) {
    let data = {
        body: post.body,
        picture: post.picture,
        type: post.type,
        user: {
            _id: post.user._id,
            name: post.user.name,
            picture: post.user.picture
        }
    }

    var headers = {
        'Content-Type': 'application/json',
        'Access-Control-Origin': '*'
    }

    return fetch(URL + "/uploadPost", {
        method: "post",
        headers: headers,
        body: JSON.stringify(data)
    })
        .then(response => Promise.resolve(response.json()))
        .catch(err => {
            return Promise.reject(err);
        })
}
Franco Coronel
  • 710
  • 1
  • 12
  • 25
  • I guess it's the response. Can you log the response to see what it is? – dvnguyen Oct 19 '18 at 00:56
  • @dvnguyen its enter in the catch and gives that error – Franco Coronel Oct 19 '18 at 01:10
  • Why can be the response? I send a small base64 and all good – Franco Coronel Oct 19 '18 at 01:34
  • Franco, the catch show the error of the conversion from response to json. That probably means that the response itself isn't a correctly formatted json object. Try to console.log(response) at the before response.json() to see what happens. – dvnguyen Oct 19 '18 at 13:56
  • @dvnguyen I understand.But it should be appearing in the database the object, because the .then is after the api call and is not appearing.Am i right? – Franco Coronel Oct 20 '18 at 14:15
  • @dvnguyen I cut the base 64 string in two and it let me send the first part alone but not second alone it says unexpected token '>' – Franco Coronel Oct 20 '18 at 15:42

1 Answers1

0

I finally solved it. The problem was that the response was returning a 413 status and I found out that means payload too large. So I added to my node js express server this lines:

var app = express();
//after
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
Franco Coronel
  • 710
  • 1
  • 12
  • 25