How to store fetched data from API to Global Variable - javascript

As seen in this picture, I have initialized variable madhu globally and assigning the fetched data to this variable inside .then method but it's showing me undefined.
How can I store this value globally?
picture

You need to understand the difference between synchronous and asynchronous code. The value is setting correctly as in line 160 after response is received but line 167 runs before 160 and at that time the value was not set.

You can do it in different options.
The simplest is using localStorage already in the HTML5, you could save the data in the browser
localStorage.setItem('response', JSON.stringify(data));
myData = JSON.parse(localStorage.getItem('response'));
sorry my english is not very good, I hope I have explained myself well

Related

Why is my console.log in LWC showing variable data in proxy handler

I'm trying to console.log a variable's value but on the browser console instead of printing the variable (an object in my case), I am getting a Proxy container with format like
Proxy {}[[Handler]]: En[[Target]]: Array(0)[[IsRevoked]]: false
On opening the [[Handler]], I get some inner properties which contains an originalTarget property.
On expanding the originalTarget , my data is shown.
How do I get this data to show properly in console and also access it in my LWC ?
this.variableName returns value in a Proxy
If you want to view proxy data so use this :
JSON.stringify(this.caseList)
And further if you want to use it in your lwc use this:
let cases = JSON.parse(JSON.stringify(this.caseList))
console.log(cases);
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Reason : whenever we mark any Javascript object as #track, Salesforce wraps the object inside creating proxy objects.
Solution: #Rajat Jaiswal answer.

How do you receive a MySQL Stored Procedure OUT parameter as a variable in Javascript?

Disclaimer; my background is in C#, so I'm not familiar with JavaScript more than at a cursory level; however, I was reviewing some code with one of our developers today and he was receiving some strange behavior of return results "merging" with pre-existing return results. I was very surprised to see every single JavaScript example of calling a Stored Procedure uses a Global Variable on the MySQL side to store the out parameter! This blows my mind and IMO is a big "no no" relative to writing code. Take the below example:
exports.updateRasSql = 'CALL update_ras_data(?, ?, #out_result, #out_result_value);
What it comes down to is we do NOT want to assign the OUT parameter variables to a MySQL global variable. We want the OUT values to be assigned to internal JavaScript variables, but any time we try to do this (using var or let as a definer), MySQL returns:
Error: ER_SP_NOT_VAR_ARG: OUT or INOUT argument 3 for routine empowercrm_main.update_ras_data is not a variable or NEW pseudo-variable in BEFORE trigger
So here's the question: How do we call a MySQL Stored Procedure from Node/JavaScript and have it return the OUT variables to internal JavaScript variables and not MySQL global variables?
You should return a result set (make query in a procedure) instead of using OUT-variables. The only way of getting the value of a OUT-variable is to select it after you have executed the procedure. It's easier just to handle the result set.

How to increment some data with LoopBack 3 without harming consistency?

I am building an API where some fields can be incremented.
After noticing data inconsistency in my MySQL database, I realized that the first version of my code was buggy:
Answer.incrementVotesCount = async (id) => {
// get a copy of the data
let answer = await getAnswer(id);
// update the copy of the data locally
answer.votesCount++;
// replace the persisted data with the updated copy of the original data
await Answer.updateAll({id}, answer);
};
Getting some data, updating it locally and persisting the modification can cause consistency problems when the route is used several times in a short period of time.
Such a situation would look something like this:
Caller A gets data. The persisted votesCount equals 14.
Caller B gets data. The persisted votesCount equals 14.
Caller A updates data. The persisted votesCount becomes 14 + 1.
At this point, the persisted votesCount equals 15, but Caller B's copy of it still equals 14.
Caller B updates data. The persisted votesCount becomes 14 + 1, whereas it should become 15 + 1.
2 increments have been performed, but the second one "crushed" the first one, since it increments an obsolete data.
I thought about using LoopBack3's native SQL functionality, but it seems like it is not fully reliable so I am unsure whether it's a good idea to use it (even though a query as simple as SET a = a + 1 should probably work correctly).
I also thought about using MySQL's triggers to perform some ACID compliant incrementing but I am unsure I can find a clean way to do this.
How do I increment some data without making it inconsistent?
I would take away the votesCount field to a separate hasOne relation, then I would make the Answer model strict='filter', so it would prevent saving data that does not really belong to the model. And when vote-up action would be taken I would increase the voteCount in that separate model, independently of the original Answer.
If you don't want to do it like this, you can try to check the original value in the before save hook, so you could get the latest value from the db and compare votesCount value from the database with the value in the model and update it accordingly.

Strange Issue with javascript setting the local storage

I want to set the local storage with unique key with "DONE" value
for example :
PdfSeen as key DONE as value
VideoSeen as key DONE as value
both
localStorage.setItem("PdfSeen" , "DONE");
localStorage.setItem("VideoSeen" , "DONE");
set a new entry in the local storage with
key DONE and value undefined
my expected result is
PdfSeen as key DONE as value
VideoSeen as key DONE as value
please check this below image to understand the issue well
the below code leads to the above entry in the localstorage
I think I found your issue. You have to pull down your bar on local storage. See the following images:
Now use the cursor to drag the line some pixels down and you will see the real keys:
undefined in the console only means, that there was no return value.
localStorage.setItem("PdfSeen", "DONE"); // will log `undefined`
console.log(localStorage.getItem("PdfSeen")); // will log the value.
also see Using the Web Storage API.

Angular refresh scope from service with initial data

I have a service returning static JSON data saving to a $scope variable like this:
$scope.workout = Workouts.get(id);
I´m manipulation this $scope.workout with shift() and slice(). When I try to set $scope.workout to the initial data from the Service, this does not work.
$scope.workout = Workouts.get(id);
The $scope.workout has been changed. I thought that another call of the service would refresh the data? Am I wrong? How to get the initial data saved in the service?
If you're using this for an edit feature, make a copy of the object to make edits to so it doesn't affect the original.
$scope.workoutTarget = Workouts.get(id);
$scope.workout = angular.copy($scope.workout);
//you're free to make edits to $scope.workout without affecting the service
In your save function, commit the changes by merging the objects.
angular.merge($scope.workoutTarget, $scope.workout)
Now $scope.workoutTarget will have the edits applied to it.
1 workaround can be, copy your $scope.workout to another Object and assign it back to $scope.workout.
$scope.workout = Workouts.get(id);
var copyObj = angular.copy($scope.workout);
The object is a reference type. So why you return it from the service, it returns it's reference. It means that you have 2 reference to the same object (from the service and in the scope). So if you change the object from one reference, it will be changed for another reference too.
You need to copy your object.
Simply you can do it by
angular.copy(Workouts.get(id), $scope.workout);
And work with your $scope.workout in your controller.
While I am not sure where your code is wrong (I am not that great at AngularJS!) A quick hack you can use is this:
Assign the initial data to to a new scope variable THEN assign your workout variable from our new scope variable. Now we can refresh workout from our new variable at any time.
You could try $route.reload to refresh the injected data.

Categories

Resources