javascriptfunctionpromiserestnode-promisify

How can return false value promise method in node if array is empty and vise versa


i was trying out promise code but it always returns me resolve even if the user does not exist in the database can anyone help me fix my code and the return statement in the return function the the second console log is only working.

here is my code

Api Call

const email = 't@t.com';
const request = require('request');
function IsUserExists(email, kc_accessToken) {
  let url = `${path}/users?email=${email}`;

  return new Promise(function (resolve, reject) {
    request(
      {
        url: url,
        headers: {
          'content-type': 'application/json',
          authorization: `Bearer ${kc_accessToken}`,
        },
      },
      function (error, response, body) {
        if (error) {
          console.log('some error occured');
        }
        if (response.body.length > 0) {
          console.log('User Exist');
          return resolve();
        }
        console.log('Does not Exist');
        return reject();
      }
    );
  });
}

Function Call

http
  .createServer(function Test() {
    getAccessToken()
      .then(function (response) {
        kc_accessToken = response.data.access_token;

        IsUserExists(email, kc_accessToken).then((resp) => {
          if (resp) {
            console.log('Do Not Create');
          } else if (!resp) {
            console.log('Creat a new User');
          }
        });
      })
      .catch(function (error) {
        // handle error
        console.log(error);
      })
      .then(function () {
        // always executed
      });
  })
  .listen(8081);

When Provided user email which exist ( t@t.com )

enter image description here

When Provided user email which does not exist( 09@t.com )

enter image description here


Solution

  • I need to create a new answer for example to you question in comments.
    Now, you go into the reject function so you need to handle this rejection in the outside.

    if (response.body.length > 0) {
        console.log('User Exist');
        return resolve();
    }
    console.log('Does not Exist');
    return reject(); // -> Now here you are
    

    You need add .catch function after IsUserExists.then().
    It will be IsUserExists.then().catch()

    http.createServer(function Test() {
        getAccessToken()
            .then(function (response) {
                kc_accessToken = response.data.access_token;
    
                // here you only accept the data from resolve in Promise
                // so you need to add .catch function to handle the rejection.
                IsUserExists(email, kc_accessToken).then((resp) => {
                    if (resp) {
                        console.log('Do Not Create');
                    } else if (!resp) {
                        console.log('Creat a new User');
                    }
                }).catch((error) => {
                    console.log(error)
                });
    
    
            })
            .catch(function (error) {
                // handle error
                console.log(error);
            })
            .then(function () {
                // always executed
            });
    })
    .listen(8081);
    

    By the way, you could add parameter in rejection function like reject(new Error("user not found)).
    Then in the outside, you can get this rejection message.