Looback error after sending data to the database using beforeRemote

0

I'm having a problem while registering the data in the database. My logic starts from the beginning where I query through a route axes for the same api, since this route corresponds to the REST verb GET, so it would return the correct information for the given account, in that case. So I would make certain validations as shown below.

But this function is async, so the function that receives it should be async tbm, as you can see beforeRemote callback gets async,

So far so good, it goes through all validations when the result is satisfied and executes the next () that it continues to save in the bank. But it returns me the error below, which for me represents an error in the create function of the loopback, however I do not know how to proceed about it.


async function getAccount(accountId) {
    return axios.get('http://localhost:3000/api/accounts/'+ accountId).then(
      (response) => {
        if(response.data.active != 1) { 
          return {error: true, codError: 23, msg: "Conta inativa" }; 
        }
        else {
          return false; 
        };
      }
    ).catch(err => {
      throw err;
      // return { error: true, codError: 24, msg: "Algo inesperado aconteceu" };
    });
  }

Customers.beforeRemote('create', async function(ctx, customers, next) {
    var resultAccount =  await getAccount(1).then(resp => resp).catch(err =>{ throw err }) ;

    if(resultAccount !== false) return ctx.res.json(resultAccount);
    if(ctx.args.data.name.split(' ').length  2) return ctx.res.json({ error: 'E-mail inválido'});
    ctx.args.data.password = bcrypt.hashSync(ctx.args.data.password, bcrypt.genSaltSync(5), null);
    ctx.args.data.active = 1;
    ctx.args.data.status = 1;
    ctx.args.data.created_at = Date.now();
    next();
  });


api-model-relational_1  | /app/node_modules/mysql/lib/protocol/Parser.js:80
api-model-relational_1  |         throw err; // Rethrow non-MySQL errors
api-model-relational_1  |         ^
api-model-relational_1  | 
api-model-relational_1  | Error: Callback was already called.
api-model-relational_1  |     at /app/node_modules/async/dist/async.js:955:32
api-model-relational_1  |     at /app/node_modules/async/dist/async.js:3871:13
api-model-relational_1  |     at interceptInvocationErrors (/app/node_modules/strong-remoting/lib/remote-objects.js:721:22)
api-model-relational_1  |     at /app/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
api-model-relational_1  |     at /app/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
api-model-relational_1  |     at /app/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
api-model-relational_1  |     at /app/node_modules/strong-remoting/lib/remote-objects.js:687:7
api-model-relational_1  |     at /app/node_modules/strong-remoting/lib/http-context.js:305:7
api-model-relational_1  |     at callback (/app/node_modules/strong-remoting/lib/shared-method.js:260:5)
api-model-relational_1  |     at /app/node_modules/loopback-datasource-juggler/lib/dao.js:452:21
api-model-relational_1  |     at doNotify (/app/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
api-model-relational_1  |     at doNotify (/app/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
api-model-relational_1  |     at doNotify (/app/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
api-model-relational_1  |     at doNotify (/app/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)
api-model-relational_1  |     at Function.ObserverMixin._notifyBaseObservers (/app/node_modules/loopback-datasource-juggler/lib/observer.js:178:5)

    
asked by anonymous 08.05.2018 / 17:36

0 answers