Search for no results in the Firebase database

Asked

Viewed 14 times

1

Hello, I’m trying to access the items contained within a user in firebase’s Altime, but the return of the snapshot is coming empty, does anyone know where the error might be?

const firebaseService = require('./firebaseService');
const {admin, customersDB} = require('./firebaseServices');
const getMd5 = require('./util').getMd5;


@param {string} email

const getUserProfile = (email) =>
  new Promise((resolve, reject) => {
    try{
      const profileRef = admin
        .database(customersDB)
        .ref(`profiles/${getMd5(email)}`);
        
      profileRef
        .once('value')
        .then((snapshot) => {
          if(!snapshot.exists()){
            return reject(new Error('usuário não foi encontrado'));
          }
          return resolve(snapshot.val());
        })
        .catch((err) => reject(err));
    } catch(error){
      return reject(error);
    }
   });

The return has always been a 'User not found!'.

1 answer

0

there is an error in the "Names chaining" eventually you don’t even need to use Reject or resolve unless you want to customize the error. I prefer to simply return the value.

const getUserProfile = (email) => {
  try {
    const profileRef = admin.database(customersDB).ref(`profiles/${getMd5(email)}`);

    return profileRef.once('value').then((snapshot) => {
      return !snapshot.exists() ? Promise.reject('usuário não foi encontrado') : snapshot.val();
    });
  } catch (error) {
    return console.log(error);
  }
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.