How to insert/update data in firebase using javascript? [closed] - javascript

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

Related

Getting data from HTML or JavaScript object? [closed]

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
Let's say I have a HTML table with 5k rows and 50 columns (generated from JavaScript object) and I would like to send 50 checked rows (checkbox) from client to server using HTTP (JSON). What would be more efficient? Iterating in HTML to find the checked rows or iterating trough my JavaScript object to find corresponding rows?
fields = columns (50)
values = rows (~5k)
JavaScript data object:
parent {
child: [{field1: value1, field2: value2, field3: value3, and so on...}]
}
I'm not sure what you are trying to do with this information, but interacting with the DOM is one of the slowest things you can do, so you should check the JavaScript objects.
While you generate each row, you keep a reference of the checkbox and you bind it to your data in a javascript object.
Then you add an event listener on the checkboxes: when you tick on or off a row, you push or remove the mapped data line in an array that will always be up to date and ready to be sent.

Using Customer.io how to send 2 different emails simultaneously to two different users at the same time. [closed]

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--;
}
}

Easier multiple value changes with jQuery [closed]

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 8 years ago.
Improve this question
I am really in need of this one, because I cann't manually type loads of options for admin panel. Hence I need a quick way to fetch each input/select and add value as I want.
Here's looong sample for one option field:
$("select[name=big_proceed]").val("true");
$("select[name=proceed_action]").val("");
$("input[name=choice_premium]").val("Premium <span>Code</span>");
$("input[name=big_proceed_pts]").val("");
$("#settings_proceed input, #settings_proceed select").each(function () {
databaseData($(this));
});
I thought something like this may work but apparently I was wrong. Here's sample:
$("#settings_proceed :input").each(function () {
$(this)
.eq(0).val("true")
.eq(1).val("")
.eq(2).val("Premium <span>Code</span>")
.eq(3).val("");
databaseData($(this));
});
Any suggestions for me ?
From the jQuery documentation:
.eq(index): Reduce the set of matched elements to the one at the specified index.
Hence your second example doesn't work as intended because $(this) only matches one element (that's the intention behind the .each()). You could rewrite the code like so:
var values = ["true", "", "Premium <span>Code</span>", ""];
$("#settings_proceed :input").each(function(i){
$(this).val(values[i]);
databaseData($(this));
});
However, this approach makes the code hard to read and error-prone because it assumes a fixed order of the HTML elements (what if you change the order of the input fields but forget to adjust the JS accordingly?). So you really should "manually" add IDs to your input elements and select them by their ID and not their index.
As #David Thomas pointed out, some sample HTML would be quite helpful here, but without knowing any further details of what you're trying to do I'd suggest the following:
var values = {
big_proceed: "true",
proceed_action: "",
choice_premium: "Premium <span>Code</span>",
big_proceed_pts: ""
};
$.each(values, function(key, value){
$("#settings_proceed").find("[name='"+key+"']").val(value);
databaseData($(this));
});
That way you can neatly define all the values in one object and let jQuery safely do the rest.

Local Variable usage reasoning [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have lot enough functions, which look like:
var functionName = function(e) {
//
};
where all the parameters are getting passed in in a single container e. Most times values are simple values (no functions), ex.:
{ parameter1: 1, parameter2: "Name", parameter3:{ subParameter1: "A"}}
But there're times when I pass in functions as in: { p2:function(){...} }
I have two options when it comes to utilising parameter values:
Options 1: get parameter values from the chain, starting from e: e.parameter1, e.parameter3.subParameter1 etc.
Option 2: use cached parameter values:
var parameter1 = e.parameter1;
var subParameter1 = e.parameter3.subParameter1;
The second option improves readability but increases the number of vars and the size of the code base. On another hand it's much drier when using long chains, i.e. e.p1.p2.p3 etc.
What reasoning should I use for choosing between those two options?
**Update 1 - the question sounds quite subjective, let me re-prase it.**
I don't mind using chains all the way, no local vars codebase is smaller, I can always figure out what's what, are the any cases when caching is a must?
A combination, based on depth(e.p1 vs e.p1.sp2.ssp3) and frequency of use. Deeper sub-properties and high usage of any sub-property both benefit from caching.
Nested property look ups can get costly, and caching the value after executing the look up once is valuable if you're going to use it a lot. This is only more efficient if you're accessing a particular property on the chain more than once, and the more you access it, the more you benefit from caching.
If you only have one level deep(e.p1, e.p2, e.p3) and you're only looking up each property value once, don't bother.
If you're accessing e.p1.sp2.ssp3 all throughout your function, cache it for sure.

creating DOM nodes from arrays [closed]

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 8 years ago.
Improve this question
Note: This is a continuation of another question that I decided were two separate issues that need to be solved. I'm also currently not sure of how exactly to phrase this question, so I will try my best and when I get more clarity I will rephrase my question for future reference.
I'm writing two basic jQuery plugins, $.fn.query and $.fn.build, that sort an array, and create html code to insert into a document, respectively. I'm currently testing it with Vimeo video ID's that I will display videos with.
$.fn.build has three parts. First it wraps every array item with individual containers, the builds them into rows (problem area), then lastly it wraps everything in a container. (every part of this is optional).
Specifically the problem comes from this line: $(tmp).add(newRow); although it is valid javascript.
if ( options.splitBy !== undefined && options.wrapRow !== undefined ) {
var tmp = $([]),
newRow = function(i) {
$(build.splice( i, i + options.splitBy )).wrapAll( options.wrapRow ).parent();
};
for (var i = 0, l = build.length, a = options.splitBy; i < l; i += a) {
$(tmp).add(newRow);
}
build = tmp;
console.log(build);
}
See: http://jsbin.com/upatus/2/edit
I am quite sure that you want to use the function, instead of adding the function itself. Also, you will want to use the same tmp object all over the time, instead of wrapping it into a new jQuery instance and not adding to the original one. Try
tmp.add(newRow(i));
BTW: If you want to build an array, you should use
var tmp = [];
and
tmp.push(…);
Now I've looked at the code from the other question. Both answers are correct, and contain some valid points:
splice is an Array function on jQuery's prototype, and returns an array. (You have fiexd this now)
Your query method returns an array, but should return a jQuery instance for chaining
Your build variable was not initialized, but used
You should really choose whether you want to use arrays or jQuery objects internally in your function, and not mix them.
BTW, you should rename your functions to more descriptive names. "build" and "query" are very vague and may collide with other plugins.

Categories

Resources