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
答案 0 :(得分:1)
我发现了我的问题:)
我在回调中加上“错误”
所以这是正确的代码
'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'}
})
};