5
respostas

Erro na no insert /pagamentos/pagamento

Estou na aula 02) Funcionalidades básicas da API de Pagamentos - 03) Persistindo o pagamento no banco

Está dando erro no insert do pagamento. Ele diz no console que há pagamento criado mas no banco não salva.

no console que digito o curl:

$ curl http://localhost:4800/pagamentos/pagamento -X POST -v -H "Content-type: a                                                                              pplication/json" -d @files/pagamento.json | json_pp
Warning: Couldn't read data from file "files/pagamento.json", this makes an
Warning: empty POST.
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
*   Trying ::1...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*                                                                               Connected to localhost (::1) port 4800 (#0)
> POST /pagamentos/pagamento HTTP/1.1
> Host: localhost:4800
> User-Agent: curl/7.54.1
> Accept: */*
> Content-type: application/json
> Content-Length: 0
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 53
< ETag: W/"35-lPNAh9F6DhSX37+tlx/h/nPQEhw"
< Date: Fri, 20 Apr 2018 00:38:56 GMT
< Connection: keep-alive
<
{ [53 bytes data]
100    53  100    53    0     0     53      0  0:00:01 --:--:--  0:00:01 53000
* Connection #0 to host localhost left intact
{
   "data" : "2018-04-20T00:38:56.266Z",
   "status" : "CRIADO"
}

no outro console diz que o pagamento foi efetuado, mas o banco não tem dado algum:

$ nodemon app
[nodemon] 1.14.11
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js app`
consign v0.1.6 Initialized in D:\ESTUDO\ALURA\API_REST_COM_NODE_AL
+ .\controllers\pagamentos.js
+ .\persistencia\connectionFactory.js
+ .\persistencia\PagamentoDao.js
Servidor rodando na porta 4800
processando a requisicao de um novo pagamento
Pagamento criado

O que pode estar acontecendo?

5 respostas

Rafaela Marraschi, tenta debugar a função que recebe o pagamento e vê se os dados estão vindo certinhos

eu coloquei um console.log da variável de conexão no método app.post , a resposta:

Servidor rodando na porta 4800 Recebida requisição de teste na porta 4800 processando a requisicao de um novo pagamento conexão = Connection { domain: null, _events: {}, _eventsCount: 0, _maxListeners: undefined, config: ConnectionConfig { host: 'localhost', port: '3306', localAddress: undefined, socketPath: undefined, user: 'root', password: 'rafaamagio', database: 'payfast', connectTimeout: 10000, insecureAuth: false, supportBigNumbers: false, bigNumberStrings: false, dateStrings: false, debug: undefined, trace: true, stringifyObjects: false, timezone: 'local', flags: '', queryFormat: undefined, pool: undefined, ssl: false, multipleStatements: false, typeCast: true, maxPacketSize: 0, charsetNumber: 33, clientFlags: 455631 }, _socket: undefined, _protocol: Protocol { domain: null, _events: {}, _eventsCount: 0, _maxListeners: undefined, readable: true, writable: true, _config: ConnectionConfig { host: 'localhost', port: '3306', localAddress: undefined, socketPath: undefined, user: 'root', password: 'rafaamagio', database: 'payfast', connectTimeout: 10000, insecureAuth: false, supportBigNumbers: false, bigNumberStrings: false, dateStrings: false, debug: undefined, trace: true, stringifyObjects: false, timezone: 'local', flags: '', queryFormat: undefined, pool: undefined, ssl: false, multipleStatements: false, typeCast: true, maxPacketSize: 0, charsetNumber: 33, clientFlags: 455631 }, _connection: [Circular], _callback: null, _fatalError: null, _quitSequence: null, _handshake: false, _handshaked: false, _ended: false, _destroyed: false, _queue: [], _handshakeInitializationPacket: null, _parser: Parser { _supportBigNumbers: false, _buffer: , _nextBuffers: [Object], _longPacketBuffers: [Object], _offset: 0, _packetEnd: null, _packetHeader: null, _packetOffset: null, _onError: [Function: bound handleParserError], _onPacket: [Function: bound ], _nextPacketNumber: 0, _encoding: 'utf-8', _paused: false } }, _connectCalled: false, state: 'disconnected', threadId: null } Pagamento criado


a minha configuração de conexão está assim:

var mysql = require('mysql');

function createDBConnection(){ return mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'payfast', port: '3306' }); }

module.exports = function() {

return createDBConnection; }


eu consigo acessar meu banco normalmente usando o mysql workbench na porta 3306, mas ele não está gravando no banco!!

Pode colocar o código que está usando a conexão?

então, finalmente consegui rodar, mas usando o mysql do xampp... Instalando o mysql puro do site não funcionou, não, é necessário alguma configuração...

obrigada pela ajuda

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software