Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a simple react app with firebase, i have my user logged in
I have a button, when is pressed i need to get the current user score in firebase ( first request) and increment this value with a random number (second request)
Get user
firebase.database().ref('user' + uid).child().once()
Update user :
firebase.database().ref('user' + uid).update({score : newValue})
Each time user press the button i made this two firebase request to get the current score and increment it
In most case user can click 150 to 600 times on the button whose can cause slowness
Do you have any suggestion to optimize my app and avoid of calling 1200 ( 2 request by 600 click) times firebase ?
why can't you try "transaction" query on the firebase.The update function takes the current state of the data as an argument and returns the new desired state you would like to write. what you have to do is just pass the key to the transaction like below
firebase.database().ref('user' + uid).child('score')
.transaction(function(current){ // current will hold current value of the key
return current + newValue ; // this return will automatically updates the new value to the firebase
});
Transaction is really good for these scenarios Follow up this link for more deets
So if you use transaction then you can reduce two queries into one.
I Hope this helps.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am working on making premium access to my website. While doing so I want the login that had set the localStorage item as 1 to expire and get deleted after a month. I searched the web, but got confused as I am still learning JavaScript and jQuery. Can you help me out in setting expiration to the localStorage item? Here's the line of code that is setting the localStorage:
localStorage.setItem('ispremium', '1');
And my website is on Blogger platform, if that matters!
You can set the actual value as the first time user joined and the time to expire. And whenever the user opens again the website, you need to check that expiration date. But this is extremely unsecure, you shouldn't do that for sessions. Just to answer to your question, you can do the following:
const NAMESPACE = 'MY_ID';
const TIMESTAMP_MODEL = {
initial: null,
expiresOn: null
};
const TIMESTAMP = Date.now();
if (!JSON.parse(localStorage.getItem(NAMESPACE))) {
// when the user access your website for the first time, set the initial and expiresOn values:
localStorage.setItem(NAMESPACE, JSON.stringify({
initial: TIMESTAMP,
expiresOn: TIMESTAMP + 1000*60*60*24*30 // 1month in ms
}));
} else {
// then, when user access the website again, check the expiresOn, it it's value is bigger than current date
const EXPIRE_DATE = JSON.parse(localStorage.getItem(NAMESPACE)).expiresOn;
if (Date.now() > EXPIRE_DATE) {
console.log('session expired');
}
}
Normally this would be solved on the server side. The localStorage may be cleared if a user selects "clear browsing history" on some browsers. It may not be available across sessions if the user works with multiple browsers or incognito mode. Other than that someone with a bit of technical knowledge can insert the "ispremium" flag easily into his localStorage to gain access to your premium feature.
If you still want to solve this via client, you could store a timestamp instead of a boolean flag and check if the current time is still in the validity range. LocalStorage itself doesn't let you set an expiration date on entries.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a firebase database where a node structure is like this.How do I insert/update data from a web page using javascript? I want to insert data individually in promo_ar,promo_en and promo_fr.
I am new in firebase and the help will be much appreciated!
You must do it this way
firebase.database().ref('Promotions/promo_en').set({
desc_promo: value,
name_promo: value
});
Remember that using set, you update elements of a node of your database, on the other hand, when you perform this type of updates, you must send both elements to update within that node, if you omit any element, it is removed from your base, for example...
firebase.database().ref('Promotions/promo_en').set({
desc_promo: value,
});
In this case, only desc_promo is being updated, thus eliminating the rest of the node elements.
in the same way you can insert new nodes in an element like this
firebase.database().ref('Promotions/promo_es').set({
desc_promo: value,
name_promo: value
});
in this way we add the Spanish language to the Promotions node
AGGREGATE:
you can have as many nodes as you want below a main node, remember that if you are going to update the final node, it does not affect the higher ones, but the higher node does affect the lower ones, for example
firebase.database().ref('Promotions/promo_es/exchange').set(
{
desc_promo: value,
name_promo: value
}
);
Promotions
|_>promo_es
|_>desc_promo : value
|_>name_promo : value
|_>exchange
|_>desc_promo : value //<-this is updated or created
|_>name_promo : value //<- this is updated or created
now if you edit the Promotions / promo_es node without including its sub nodes, these will be eliminated, you can perform the tests that you deem convenient in firebase to polish the method to use in your system
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I use localstorage to save items (songs) as favorites. But I would also like to be able to remove the items from the array, so not the key but the value of the key.
for example I have the key Songs and values of one , two, three. I would like to be able to remove two from Songs but one and three should remain.
I cant seem to figure out how to do so because all I can find is how to remove the key and not the value of the key.
Since you don't have any source code on display I will give you quick example of how to remove single values from the clients Local Storage. You can expand on this to remove multiple values. Wouldn't want to remove all the fun
I have commented out the Localstoage and created a Songs array so this will work in the snippet.
Remove /* and */ to uncomment that section and change getItem('Songs') to fit your current source code.
You will also want to remove var Songs[]; Snippet Use only
/*--Local Storage Use
// Get Songs from Local Storage
var Storage = localStorage.getItem('Songs');
//Split Songs into an Array
var Songs=Storage.split(',');
*/
// Snippet Use -- Example Values of the Songs Result
var Songs = ['Song One', 'Song Two', 'Song Three','Song Four'];
//------ Remove Single Value
var Remove = Songs.indexOf('Song Two');
alert('Old Songs List:\n'+Songs);
if (Remove > -1) {
Songs.splice(Remove, 1);
}
alert('New Songs List:\n'+Songs);
//--Update LocalStorage
//localStorage.setItem('Songs',Songs);
If you have any questions about the above example please leave a comment below and I will get back to you as soon as possible.
I hope this helps. Happy coding!
Assuming your Object is structured like this;
"Songs": ["Song One", "Song Two", "Song Three"]
I don't have the code for you, but the process I would go through is;
Read localStorage item (localStorage.getItem('Songs'))
If you know the index of the item, you can then remove it. See this post for details
Re-save the Object to localStorage (localStorage.setItem('Songs', SongsObj)).
You need to get value of localStorage (Songs for example). Save value into variable, then change value (delete/remove some list items), then uses same key (Songs) set previous localStorage key with new value.
localStorage.setItem('Songs', [1,2,3,4,5]);
var songs = localStorage.getItem('Songs');
// ... some operations with list...
songs = [1,3,4];
localStorage.setItem('Songs', songs);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
As the title states, I want to send two different emails, simultaneously to two different users.
I am using JavaScript for the API.
I also would like to know how can I send delayed emails, on custom dates. Currently we can do that from the dashboard, but that allows to fix days / weeks. But what I'm looking for is to calculate the dates and add a custom date delay that can't be predefined as given in the dashboard.
Thank you.
Well the problem has been solved.
I created two separate API calls for each customer. Using switch statement and while loop
switch(customer){
while(counter > 0){
case 1:
//Make first API call
//_cio.identify
_cio.identify({
// Required attributes
id: id, // Unique id for the currently signed in user in your application.
email: yourEmail, // Email of the currently signed in user.
created_at: time, // Timestamp in your system that represents when
// the user first signed up. You'll want to send it
// as seconds since the epoch.
// Optional (these are examples. You can name attributes what you wish)
task: task,
supervisor_email: supervisorEmail, // Add any attributes you'd like to use in the email subject or body.
goal_date: date, // To use the example segments, set this to 'free' or 'premium'.
user_name: name,
supervisor_name: supervisorName,
goal_setter: isUser,
pay: pay
});
customer = 2;
break;
case 2:
//Make the second API call
break;
counter--;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The code below belongs to a crossrider extension I am currently attempting to develop that hopefully will sync bookmarks across browsers. This file is currently the background.js file it will first retrieve a snapshot of the bookmarks file from the local database then compare that to the current list of bookmarks and if there are any differences (either additions to the bookmarks list or subtractions) they will be be returned with the getChanges() function and then sent to the server, finally the script updates the snapshot and waits 30 seconds before restarting the process. I dont really know how to make the getChanges() function. It needs to return presumably a json object indicating both the additions and subtractions (both their titles and urls). If someone could write the code for the function that would be great. Thanks
appAPI.ready(function() {
// Poll every 30 seconds
setInterval(function() {
appAPI.db.async.get('prevBookmarks', function(value) {
// Load or initialize previous bookmarks list
var prevBookmarks = (value) ? value : {};
// Get current bookmarks
appAPI.bookmarks.getTree(function(nodes) {
// Save bookmark list for next comparison
appAPI.db.async.set('prevBookmarks', nodes);
// In your getChanges functions, traverse the bookmark trees collating
// changes and then post then to your API server using appAPI.request
var changes = getChanges(prevBookmarks, nodes);
appAPI.request.post({
url: http://yourAPIserver.com,
postData: changes,
contentType: 'application/json'
});
});
});
}, 30 * 1000);
});
Ok, you've got jQuery as one of your tags, so try this link: Compare 2 arrays which returns difference.
It returns the differences between two arrays. You'll have to perform this twice for what you're doing, once to figure out what is in current that is not in previous and vice versa. I don't know what properties are contained in your bookmarks so this simple example might not exactly suit your needs, but it might point you in the right direction.
Good luck and welcome to JavaScript!