Search code examples
node.jspromisees6-promise

JS: ERROR: "Promise { <pending> }" with Sockets


I'm having a problem with a Promise, when I try to retrieve the data it says Promise { <pending> } I already looked for some information about that but I cannot understand how to end it. If anyone can help me I would be appreciated.

Thanks in advance

    /bucket.js
    'use strict'

    const   connection  = require('../server/models'),
            oracledb    = require('oracledb'),
            conexion    = oracledb.getConnection(connection)

    oracledb.outFormat  = oracledb.OBJECT

    module.exports  = (data) => {
      console.log("RES: ", filter(data));
      return filter(data)
    }

    const filter  = (value) =>  {
        return conexion
        .then(con =>  {
          return con.execute(
            `SELECT id_application, name, description, creation_date ` +
            `FROM application `
          ).then(bucket =>  {
            return con.execute(
              `SELECT id_definition, id_application, field_name_original,
               field_name_new, column_name, position, id_type_data,
               field_size, creation_date, description, filter, visible ` +
              `FROM definition ` +
              `WHERE id_application in (${getApp(value.data)}) ` +
              `AND ${value['search']} = '${value['value']}' `
            ).then(definitions => { return creaJSON(bucket, definitions) } )
             .catch(error =>  { return {"error": error} })
          })
           .catch(error =>  { return {"error": error} })
        })
        .catch(error =>  { return {"error": error} })
    }

    const getApp = (value) =>  {
      return value.map(obj =>  {
        return `'${obj.ID_APPLICATION}'`
      })
    }

    const creaJSON = (buckets, definitions)  => {
      var df = new Array()
      buckets['rows'].map(obj =>  {
        definitions['rows'].map(def =>  {
          if(obj['ID_APPLICATION'] == def['ID_APPLICATION']) df.push(def)
        })
        obj['Definitions'] = df
        df = []
      })
      return buckets.rows
    }

UPDATED

My error wasnt only in the code above. I'm using sockets and also with the Bergi answer I couldn't had my response in the client. I had the next code:

socket.on('bucketVisibleT', (data)  =>  {
  buckets = {data:data,search:'VISIBLE',value:'T'}
  io.sockets.emit('bucketVisibleFs', require('./bucket')(buckets))
})

so I had to change with the next one, and now I have my response in my client.

socket.on('bucketVisibleT', (data)  =>  {
  buckets = {data:data,search:'VISIBLE',value:'T'}
  require('./bucket')(buckets).then(res => {
    io.sockets.emit('bucketVisibleTs', res)
  })
})

I apologise because I didn't give a good explanation of my problem but I thought it was in my first code


Solution

  • filter returns a promise, so like with the other promise-returning functions you need to chain a then call to it if you want to do something with the result:

    module.exports = (data) =>
      filter(data).then(res => {
        console.log("RES: ", res);
        return res;
      });