Subiyantoro
Subiyantoro

Reputation: 85

Unhandled error for request POST Loopback JS

help me please, i'm trying to create remote method on loopback js, when my function returning callback, showing error "Unhandled Error for request POST" and make my respone code return to 500 response code, but still showing my result data.

Here is my code

'use strict';

// Define Variable Depedencies Here
var jwt = require('jsonwebtoken')

module.exports = function(Account) {
    Account.login = function(username, password, cb) {
        var data = {
            username: username,
            password: password
        }
        var token = jwt.sign({exp: Math.floor(Date.now() / 1000) + (60*60), data: data}, 'secret');
        cb(token)
        // console.log(token)
    }
    Account.remoteMethod('login', {
        description: ['Login With Your Credentials'],
        http: {path: '/login', verb: 'post'},
        accepts: [
            {arg: 'username', type: 'string'},
            {arg: 'password', type: 'string'}
        ],
        returns: {arg: 'token', type: 'string'}
    })

};

and this my error :

D:\PROJECT\Backend>node .
Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer
Unhandled error for request POST /api/Accounts/login: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDE4NTM2MTYsImRhdGEiOnsidXNlcm5hbWUi
OiJhZG1pbiIsInBhc3N3b3JkIjoiMTIzNDUifSwiaWF0IjoxNTAxODUwMDE3fQ.os6ijfYyF8losGywdnrVKrHW3-DYZFSwlOVUvHyPIOk

Thanks in advance

Upvotes: 1

Views: 3755

Answers (1)

Subiyantoro
Subiyantoro

Reputation: 85

I Found my Problem :)

i put "err" on callback

so this the right code

'use strict';

// Define Variable Depedencies Here
var jwt = require('jsonwebtoken')

module.exports = function(Account) {
    Account.login = function(username, password, cb) {
        var data = {
            username: username,
            password: password
        }
        var token = jwt.sign({exp: Math.floor(Date.now() / 1000) + (60*60), data: data}, 'secret');
        cb(err, token)
        // console.log(token)
    }
    Account.remoteMethod('login', {
        description: ['Login With Your Credentials'],
        http: {path: '/login', verb: 'post'},
        accepts: [
            {arg: 'username', type: 'string'},
            {arg: 'password', type: 'string'}
        ],
        returns: {arg: 'token', type: 'string'}
    })

};

Upvotes: 3

Related Questions