I
I
Ivan Belovikov2020-12-30 19:56:47
JavaScript
Ivan Belovikov, 2020-12-30 19:56:47

How to store local data in a variable?

I welcome everyone!
Congratulations to everyone on NG or almost NG, I don’t know how much you have left, 48, 24, 12 hours.
Okay, comrades, I have such a problem. I get data from the database, everything is saved, but when I display the variable of the main function (obj), in the function where I save it, everything is fine, but where the return (at the end of the function) is for some reason an empty object ... How so?
Here is the function code:

const findOrCreate = (id) => {
      const token = cookie.parse(socket.request.headers.cookie)['token']
      let obj = []
      if (!token) {
        obj = { username: username, id: socket.id, position: { lat, lng }, selected: 'man', data: '', country, avatar: ava_path }
        usr.pus(res)
      } else {
        const db = new PouchDB('http://127.0.0.1:5984/pp4_db')
        db.get('user')
        .then(function(doc) {
          const user_d = JSON.parse(doc.user)
          obj = user_d
        })
        .catch(function (err) {
          return err
        })
      }
      console.log(obj)
      return users.find(user => user.id === id) || createUser(obj)
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-12-30
@arat1337

Read about asynchrony. Thousands of questions on the toaster, hundreds of articles on Google.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question