Avoid assigning variables outside of their intended scope in Javascript

I'm struggling to figure out why the assignment of vm.Info variable isn't working as expected. Apologies for the unclear question.

 
vm.getUser = function(email) {
             vm.Info = ''  

                authentication
                    .avvocatoByEmail(email)
                    .error(function(err){
                        return null;
                    }).success(function(data){
                        vm.data = { user : data };

                        vm.Info = $crypto.encrypt(vm.data.user[0].id + "." + vm.data.user[0].email, 'myKey');
                        console.log("Seems fine here: " + vm.Info);

                    });

                console.log("Looks empty here: " + vm.Info);
            }

Answer №1

The asynchronous nature of the ajax call causes the second console.log (currently not functioning) to be executed before the first one. Once the success event of the ajax call is triggered, the first console.log will work as intended when assigning data to the variable.

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

Create a Jest unit test for a specific function

I started my first unit test in react with jest this afternoon. The initial 5 tests I need to do involve testing the return functions, which isn't too difficult. However, I'm struggling to understand how to perform unit testing on my login funct ...

Discover the secret to revealing hidden text as it seamlessly transitions through other text

I am working on a simple transition animation where I need to hide text (A href) initially using "display: none" and then make it visible with "display: block" after an image passes through it, triggered by a click event in JavaScript. Check out my code ...

converting blob to base64 in javascript

I need assistance with decoding a blob object to a base64 string using JavaScript. Here is the code I am working with: var reader = new FileReader(); reader.addEventListener("loadend", function () { // Accessing contents of blob as a typed array ...

Does Notepad++ only paste the first line of copied code when using the replace feature?

I frequently utilize the replace feature in Notepad++ to change code across multiple files. However, I've encountered an issue where when I try to paste code with multiple lines into the replace textbox of Notepad++, only the first line gets pasted. T ...

Ways to access the req.user object within services

After implementing an authentication middleware in NestJs as shown below: @Injectable() export class AuthenticationMiddleware implements NestMiddleware { constructor() {} async use(req: any, res: any, next: () => void) { const a ...

What is the reason for the lack of arguments being passed to this Express middleware function?

I have been developing a middleware that requires the use of `bodyParser` to function, however I do not want to directly incorporate it as a dependency in my application. Instead, I aim to create a package that includes this requirement and exports a middl ...

Using inline SVG in a Vite production build

When utilizing Vite and Vue, my goal is to compile a single JavaScript file without creating an assets directory that includes SVG and GIF files during the build process. I want to achieve this without manually inserting the SVG code in a Vue JS file as a ...

What is the purpose of requiring the explicit invocation of app.listen(port) to enable express-ws to function properly?

I've recently started exploring NodeJS Express and came across the official tutorial from express-ws for setting up websockets in a simple project generated using npx express-generator. While following the tutorial, I noticed that in the app.js file, ...

Looking to enforce limits on the values and character counts within input boxes with JavaScript

Having issues with my code for restricting input box entry to only numbers and limiting the characters per box to 3, 3, and 4 respectively. Despite my efforts, I can't prevent users from entering special characters or restrict the character limits as ...

Tips for creating a blur effect on all options except for the selected 2 out of 4 using jQuery

My goal is to choose 3 options from a list and once 3 options are selected, all other options will be blurred to prevent further selection. Please review the code snippet I have provided below. Link to File <!DOCTYPE html> <html> <head> ...

Tips for Altering Text Color within a String with Javascript or Jquery

This seems like a straightforward issue, however I am struggling to figure it out. <p><span="test1">This is some test</span> text to color.</p> I am trying to change the color of the "to color" characters using JavaScript or jQuer ...

Display a fixed three levels of highchart Sunburst upon each click in Angular8

Looking to create a dynamic sunburst highchart that displays three levels at a time while allowing interactive drilling. For instance, if there are 5 levels, the chart should initially show the first three levels. When clicking on level 3, levels 2, 3, and ...

develop a submission form using jquery

I am looking to create a submit action where clicking the submit button does not refresh the page. Instead, the data inputted in div1 will be sent to kanjiconverter.php and displayed in div2 using <?php echo $newkanji ?>. There are three forms on thi ...

Issue with Angular ng-Model not appearing on the second attempt

Within a modal popup that appears based on certain conditions, everything functions normally the first time it is displayed. The code in question is as follows: <div class="form-group"> <p>First Name:</p> {{vm.user.name_first}} ...

Transform the checkbox selection into an active hyperlink upon being selected

Currently, I am in the process of designing a form that allows visitors to select items they would like to view by checking relevant checkboxes. Upon clicking a Request button, the URLs of the selected items are displayed. I aim to have the URL appear as ...

The SetInterval function is failing to display the current state

Even though the state has been set to true before calling the setInterval function, the useEffect hook is triggered with the new value of the state but it's not being reflected in the setInterval function. https://i.sstatic.net/xdrW4.png Check out th ...

Discover the secret to applying a gradient shade to the "border" element when it reaches full capacity with Vue's innovative Custom CSS package

Utilizing the package https://www.npmjs.com/package/vue-css-donut-chart#usage-with-all-the-available-props to create a "border" effect around images based on progress has presented a challenge. Specifically, achieving a gradient color for the border when i ...

Unexpected outcomes when rigging a .dae model in Three.js with Blender

I have been working on creating a rigged model for use in a browser using Three.js. Before rigging the model, it loads perfectly fine, allowing me to move and rotate it without any issues. However, after rigging the model, the pieces load in different loca ...

Issue: The variable THREE has not been defined within the FBXLoader code

Currently, I am utilizing the module version of three.js in order to execute imports from a client-side JavaScript file structure. The core functionality of THREE is working correctly. However, I am encountering an issue when attempting to use it with FBX ...

When attempting to incorporate a video using Sencha, an error occurs stating that the resource is being interpreted as an image but is being

While attempting to incorporate a sample into Sencha, I encountered an error stating "Resource interpreted as Image but transferred with MIME type text/css". Ext.define('MyApp.view.Newpage', { extend: 'Ext.Video', xtype: ' ...