0

This is my first time using promises and mongoose, and I'm trying to save data to global variable for later use

const getUser = async () => {
    let user 
    try {
       user = await clientModel.findOne({username: email})
       consoe.log(user) 
    } catch (e) {
      console.log(e)
    }
    return  user
  }

  const filteredUser = getUser().then((value) => {
    return value
  }).catch((e) => console.log(e));

  console.log(filteredUser)

user console log displays content:

{
  _id: new ObjectId("61aa75c64e1526131d98f2a1"),
  username: 'paul@beatles.uk',
  chequing: null,
  saving: '1000022',
  __v: 0
}

but filteredUser keeps displaying Promise { <pending> }

durak_chtoli
  • 61
  • 2
  • 7
  • All `async` functions return a promise. The caller must use `await` or `.then()` to get the value from that promise. See [Why do I need to await an async function](https://stackoverflow.com/questions/56895695/why-do-i-need-to-await-an-async-function-when-it-is-not-supposedly-returning-a-p/56895719#56895719) for explanation. – jfriend00 Dec 03 '21 at 20:26

1 Answers1

0

You need to resolve promise for use data. Read about that here

It can be await or then

Andrew
  • 66
  • 3