"The concept of hoisting in JavaScript and its impact on global

Is there a way to set the variable "username_show" from this PouchDB document? I attempted using hoisting, but it seems that I may need to move the global variable outside of the data structure and then outside of the function.


    var db = new PouchDB('myDb');
    var remoteCouch = false;
    var username_show;

    function loadSettings(){    
        var settings = {
            _id: "UNa",
            username : ""
        }

        db.get('UNa').then(function (doc) {
            var username_show=doc.username; 
        });
    }

    loadSettings();
    alert("The Username="+username_show);

Answer â„–1

Your notification triggers before username_show is defined because it is being set asynchronously within a promise's result.

When working in your loadSettings function, refrain from using the var keyword when accessing your global username_show variable.

To access the variable after it has been assigned, consider invoking a function within your promise.

var db = new PouchDB('myDb');
var remoteCouch = false;
var username_show;

function loadSettings(){        
  var settings = {
    _id: "UNa",
    username : ""
  }


  db.get('UNa').then(function (doc) {
    username_show=doc.username; 
    showUsername();
  });

}

function showUsername() {
  alert("The Username="+username_show);
}

loadSettings();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to use jQuery to adjust the text color once it has been clicked on?

I have been struggling to change the text color upon clicking on it without any success. Within my code, there are seven labels - one for the question, four for the answer options, one for the correct answer, and the last one for the explanation. The desi ...

Traverse through the loop with React Material UI in JavaScript

Hi everyone, I'm having trouble with this code. I want to iterate through an object called paleogenome.data and create a CardHeader for each item: { Object.keys(paleogenome.data).forEach(function (key){ console.log(paleogenome.data[key] ...

Utilizing AJAX to invoke a function

I'm having trouble explaining this, but I'll try my best. I created a function that calculates the remaining amount needed to qualify for free delivery based on different basket value thresholds. Whenever a product is added to the basket, the b ...

Having trouble with the placeholder blur feature on images in Next.js?

Within my website, I have a dynamic route set up as /titles/[slug].js. Upon initially navigating to this route, everything functions as expected - the placeholder blur effect displays on all images and animations triggered by react-intersection-observer wo ...

Express JS: Condition to pause execution for 50 seconds until receiving the firestore query response, otherwise proceed with the other process

I am working on a project to create a simple application that will wait for 50 seconds for an API or database call. If it does not receive a response within the specified timeframe, it will discard the call and move on to the next one. async (collection, d ...

What is the best method to incorporate a JavaScript object key's value into CSS styling?

I am currently working on a project in React where I'm iterating over an array of objects and displaying each object in its own card on the screen. Each object in the array has a unique hex color property, and my goal is to dynamically set the font co ...

Does __ only function with curried functions as intended? What is the reason for it working in this case?

I'm trying to figure out the reason behind the successful usage of __ in this particular code snippet : function editAddress (id, addressId, model) { return BusinessService .getById(id) .then(unless( () => checkUrlValue(add ...

"Pressing the 'back' button on your browser takes

Is there a way to navigate back to the main page by clicking on an image? After selecting First, Picture1 will be shown. How can I go back to the selection page for further choices? <a href="picture1.jpg"> <h3>First</h3></a> <a ...

What is the method for identifying the name of a CSS Variable currently in use?

Consider this code snippet: /* css */ :root { --text-color: #666666; } input { color: var(--text-color); } In Javascript, how can I determine the name of the CSS custom property (variable) being utilized? // javascript console.log(document.querySel ...

Unable to properly delete data in Express/Postgres

After developing a Kanban board using JavaScript without any frameworks on the front end, I integrated Express/Postgres on the back end. However, I am encountering an issue with the 'Delete' operation. While all other CRUD operations are functio ...

MUI Step Indicator Icon is not visible

I'm having trouble getting the MUI Stepper Component to display Icons within each step node. Despite following the sample code provided by MUI, my implementation only shows a blank screen instead of the desired look. The desired appearance includes i ...

Ways to automatically update property value in MongoDB once a particular date is reached

Is it feasible to schedule a future date for a document in MongoDB, such as 30 days from the current date, and then automatically update another property of the document when that future date arrives? For instance: creating an event document setting the ...

Is there a way to sum up the values in my object without having to use Object.entries within?

Here is a snippet from my JSON data: const jsonData = { "08/23/2022": { "One": 254, "Two": 92, "Three": 8 }, "08/13/2022": { "One": 327, "Two": 86, }, " ...

parsing a TypeScript query

Is there a simpler way to convert a query string into an object while preserving the respective data types? Let me provide some context: I utilize a table from an external service that generates filters as I add them. The challenge arises when I need to en ...

Having trouble with a Three.JS/WebGL 3D object not loading in Firefox?

After creating a 3D Object using Three.js examples, I noticed that it works perfectly in Chrome and IE 11, but for some reason, it's not loading on Firefox. I am currently using the latest version of Firefox (FF 27.0). When I tested the code on Firef ...

Issues with setting headers after they have been sent - Can you explain why?

How am I setting a header after it has been sent to the client? Here is the code snippet: When a form is submitted, a post ajax request is triggered which returns a JSON object to the client. I have commented out most of the code to troubleshoot, and cur ...

Utilizing jQuery for AJAX to send data variables

Is it feasible to pass a variable to jQuery using the .ajax method? Instead of the traditional way, can I do something like this: <button onclick="sendQuery()"> function sendQuery(){ $.ajax({ type: "GET", url: "action.php", ...

When an external image is dragged over, include the class in the drop area of the file input

Hey, does anyone know how to use Jquery to add a class when an element is being dragged over a drop area? Here's the HTML code I'm working with: <div class="upload"> <form action=""> <input class="uploadfile" type="file" ...

How to obliterate an array element in JavaScript

Hello everyone, I am faced with an array of objects that I need to destructure. Below is a snippet from the array: [ { "Area": "Werk Produktivität [%] - Target", "Jan": 86.21397507374327, "Feb": 86.057 ...

Python: Assigning a variable globally

If we have the following construct: x = "" def xy(): x = "String" After calling xy() and then printing x, it will still be empty. Attempting to declare the variable as global with the following code: x = "" def xy(): global x = "String" Resul ...