1
resposta

SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (<anonymous>)

Deu tudo certo, até o ultimo minuto do video, quando eu fui colocar os dados novamente. ERRO QUE MOSTRA NO SERVIDOR

consign v0.1.6 Initialized in C:\Users\YuriPNP\Desktop\Alura\payfast
+ .\controllers\pagamentos.js
servidor rodando na porta 3000
SyntaxError: Unexpected token ' in JSON at position 0
    at JSON.parse (<anonymous>)
    at createStrictSyntaxError (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\body-parser\lib\types\json.js:158:10)
    at parse (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\body-parser\lib\types\json.js:83:15)
    at C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\raw-body\index.js:224:16)
    at done (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\raw-body\index.js:273:7)
    at IncomingMessage.emit (events.js:202:15)
    at endReadableNT (_stream_readable.js:1132:12)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)

ERRO QUE APARECE QUANDO INSERIR OS DADOS PELO TERMINAL

C:\Users\YuriPNP\Desktop\Alura\payfast>curl http://localhost:3000/pagamentos/pagamento -X POST -v -H "content-type: application/json" -d '{"formaDePagamento = "payfast", "valor":10, "moeda":"BRL", "descricao": "criando um pagamento"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /pagamentos/pagamento HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.55.1
> Accept: */*
> content-type: application/json
> Content-Length: 69
>
* upload completely sent off: 69 out of 69 bytes
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< Content-Security-Policy: default-src 'self'
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 1134
< Date: Wed, 20 Mar 2019 18:04:16 GMT
< Connection: keep-alive
<
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token &#39; in JSON at position 0<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at createStrictSyntaxError (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\body-parser\lib\types\json.js:158:10)<br> &nbsp; &nbsp;at parse (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\body-parser\lib\types\json.js:83:15)<br> &nbsp; &nbsp;at C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\body-parser\lib\read.js:121:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\raw-body\index.js:224:16)<br> &nbsp; &nbsp;at done (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\raw-body\index.js:213:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\YuriPNP\Desktop\Alura\payfast\node_modules\raw-body\index.js:273:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:202:15)<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1132:12)<br> &nbsp; &nbsp;at processTicksAndRejections (internal/process/next_tick.js:76:17)</pre>
</body>
</html>
* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: um/
* Could not resolve host: um
* Closing connection 1
curl: (6) Could not resolve host: um
curl: (3) [globbing] unmatched close brace/bracket in column 10

MEU CÓDIGO

var express = require('express'); // carregando o aqrquivo
var consign = require('consign'); // adiminstrador de pastas e rotas
var bodyParser = require('body-parser');


module.exports = function(){
  var app = express(); // referencia do objeto express
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(bodyParser.json());

  consign()
  .include('controllers')
  .into(app)
  return app;
}

Alguem me ajuda, não sei o que pode ser e não consigo sair desse vídeo, pq sempre dá esse erro

1 resposta

Fala aí Yuri, tudo bem? Provavelmente é algum problema no JSON que você está mandando para a API.

Consegue me mandar o JSON? Dessa maneira ficaria mais fácil para eu tentar lhe ajudar.

Fico no aguardo.