Sava Data to a variable Place [Firebase] - javascript

I want to write some User Data to a variable place. But I don't know how I can tell this to Firebase. The normal save function doesn't work with a variable save location. Like in the example below I want to save the data on a variable save location.
function Writevariabledata(userId,data)
{
firebase.database().ref('user/testdata/' + userId).set({
$userId: data
});
}

this works for me
function Writevariabledata() {
var name = document.getElementById("name");
var firebaseref = firebase.database().ref("user/testdata/" + userId);
var messagetext = {};
messagetext.cat_name = name.value;
firebaseref.push().set(messagetext);
window.alert('Inserted Successfully..');

Related

ServiceNow UI Page GlideAjax

I created a form using UI Page and am trying to have some fields autopopulated onChange. I have a client script that works for the most part, but the issue arises when certain fields need to be dot-walked in order to be autopopulated. I've read that dot-walking will not work in client scripts for scoped applications and that a GlideAjax code will need to be used instead. I'm not familiar with GlideAjax and Script Includes, can someone help me with transitioning my code?
My current client script looks like this:
function beneficiary_1(){
var usr = g_user.userID;
var related = $('family_member_1').value;
var rec = new GlideRecord('hr_beneficiary');
rec.addQuery('employee',usr);
rec.addQuery('sys_id',related);
rec.query(dataReturned);
}
function dataReturned(rec){
//autopopulate the beneficiary fields pending on the user selection
if(rec.next()) {
$('fm1_ssn').value = rec.ssn;
$('fm1_address').value = rec.beneficiary_contact.address;
$('fm1_email').value = rec.beneficiary_contact.email;
$('fm1_phone').value = rec.beneficiary_contact.mobile_phone;
var dob = rec.date_of_birth;
var arr = dob.split("-");
var date = arr[1] + "/"+ arr[2] + "/" + arr[0] ;
$('fm1_date_of_birth').value = date;
}
}
fm1_address, fm1_email, and fm1_phone do not auto populate because the value is dot walking from the HR_Beneficiary table to the HR_Emergency_Contact table.
How can I transform the above code to GlideAjax format?
I haven't tested this code so you may need to debug it, but hopefully gets you on the right track. However there are a couple of steps for this.
Create a script include that pull the data and send a response to an ajax call.
Call this script include from a client script using GlideAjax.
Handle the AJAX response and populate the form.
This is part of the client script in #2
A couple of good websites to look at for this
GlideAjax documentation for reference
Returning multiple values with GlideAjax
1. Script Include - Here you will create your method to pull the data and respond to an ajax call.
This script include object has the following details
Name: BeneficiaryContact
Parateters:
sysparm_my_userid - user ID of the employee
sysparm_my_relativeid - relative sys_id
Make certain to check "Client callable" in the script include options.
var BeneficiaryContact = Class.create();
BeneficiaryContact.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getContact : function() {
// parameters
var userID = this.getParameter('sysparm_my_userid');
var relativeID = this.getParameter('sysparm_my_relativeid');
// query
var rec = new GlideRecord('hr_beneficiary');
rec.addQuery('employee', userID);
rec.addQuery('sys_id', relativeID);
rec.query();
// build object
var obj = {};
obj.has_value = rec.hasNext(); // set if a record was found
// populate object
if(rec.next()) {
obj.ssn = rec.ssn;
obj.date_of_birth = rec.date_of_birth.toString();
obj.address = rec.beneficiary_contact.address.toString();
obj.email = rec.beneficiary_contact.email.toString();
obj.mobile_phone = rec.beneficiary_contact.mobile_phone.toString();
}
// encode to json
var json = new JSON();
var data = json.encode(obj);
return data;
},
type : "BeneficiaryContact"
});
2. Client Script - Here you will call BeneficiaryContact from #1 with a client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var usr = g_user.userID;
var related = $('family_member_1').value;
var ga = new GlideAjax('BeneficiaryContact'); // call the object
ga.addParam('sysparm_name', 'getContact'); // call the function
ga.addParam('sysparm_my_userid', usr); // pass in userID
ga.addParam('sysparm_my_relativeid', related); // pass in relative sys_id
ga.getXML(populateBeneficiary);
}
3. Handle AJAX response - Deal with the response from #2
This is part of your client script
Here I put in the answer.has_value check as an example, but you may want to remove that until this works and you're done debugging.
function populateBeneficiary(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON(); // convert json in to an object
// check if a value was found
if (answer.has_value) {
var dob = answer.date_of_birth;
var arr = dob.split("-");
var date = arr[1] + "/"+ arr[2] + "/" + arr[0];
$('fm1_ssn').value = answer.ssn;
$('fm1_address').value = answer.address;
$('fm1_email').value = answer.email;
$('fm1_phone').value = answer.mobile_phone;
$('fm1_date_of_birth').value = date;
}
else {
g_form.addErrorMessage('A beneficiary was not found.');
}
}

Firebase - return user uid and set user meta data value to different node

I am trying to retrieve data from a Users node in Firebase and use the retrieved data in a set() to another node. Specifically I want to use my user UID to look at which "branch" the user belongs to and then use the value in the set()
I have the following snapshot to get the "branch" of the user. It logs to the console the correct value
var ref = new Firebase("firebase/url");
var authData = ref.getAuth();
var userUID = authData.uid
var userRef = new Firebase("firebase/URL" + userUID);
userRef.once("value", userBranch)
function userBranch(snapshot) {
var data = snapshot.val();
console.log(data.branch);
};
With the returned "branch" value I want to use it in the following set()
var postsRef = ref.child("Posts");
var newPostRef = postsRef.push();
var postID = newPostRef.key();
postsRef.child(postID).set({
branch: data.branch})
What is the best way to take the value out of the function userBranch so I can use it in my set()?
You are using .once a little bit wrong, but you have the right idea.
var ref = new Firebase("firebase/url");
var authData = ref.getAuth();
var userUID = authData.uid
var userRef = new Firebase("firebase/URL" + userUID);
// Second parameter is callback function that receives data (snapshot)
userRef.once("value", function(snapshot) {)
var data = snapshot.val();
console.log(data.branch);
// Save value at data.branch
var dataBranch = data.branch;
var postsRef = ref.child("Posts");
var newPostRef = postsRef.push();
var postID = newPostRef.key();
// Save it, erasing all previous data at location
postsRef.child(postID).set({
"branch": dataBranch
});
};
You were a little mixed up with the second parameter for .once.
References:
Firebase | .push
Firebase | .once
Firebase | .set

Use non-static variable with chrome.storage.local.set / get

Is it possible to use a non static variable with chrome.storage.local.get. Right now, I can not recover anything.
here's what I do:
myVar = 'test';
var obj = {};
obj[myVar] = Output;
storage.set(obj);
and to recover, I do:
var key = example + 'js';
storage.get(key, function (result) {
console.log (result.key)
});
Thank you in advance for your help!
You can pass null as the query to retrieve the whole storage:
chrome.storage.local.get(null, function(data) {
// data contains everything in storage
});

how to use form data saved in an array in another html file

im having a project in which there is an html file for sign up and another one for login. So i am storing the data from the sign up form in some arrays and i want to use them in the login file but the arrays seems to be empty. Is there somekind of global array in javascript or something like that? Thanks in advance :) My javascript code is:
var usernames=[]; //login usernames
var passwords=[]; //login passwords
var personUsr = []; //signup usernames
var personPass = []; //signup passwords
function insert(){ //login onclick function
var usernameValue = document.getElementById('username').value;
var passwordValue = document.getElementById('password').value;
usernames[usernames.length]=usernameValue;
passwords[passwords.length]=passwordValue;
document.getElementById('username').value="";
document.getElementById('password').value="";
for(var i = 0; i < personUsr.length; i++) {
if(titleValue===personUsr[i]){
alert("user found"); }
else{
alert("no user with that username");}
}
}
function submitSignUp(){ //signup onclick function
var firstName = signupform.elements["firstname"].value;
var lastName = signupform.elements["lastname"].value;
var country = signupform.elements["countrySel"].selectedIndex.value;
var user = signupform.elements["username"].value;
var pass = signupform.elements["password"].value;
var repass = signupform.elements["password2"].value;
personUsr[personUsr.length]=user;
personPass[personPass.length]=pass;
}
AJAX is built for this purpose. If you want to post javascript variable to other page you should use ajax.
Else you can use a sever-side technique to do this

Meteor insert record using form values

First of all, I'm a complete JS novice. I'm experimenting with Meteor.My objective is to to build a simple form that inserts records into a table. I've set up variables to grab values from each input, and I've placed those variables into an insert method. When I click the button, it recognizes the click, but doesn't pull any values from the inputs. I'm sure I'm missing something simple here, I just can't figure out what it is.
Here's the JS:
var Leads = new Meteor.Collection("Leads");
if (Meteor.is_client) {
Template.Leads.LeadsArr = function(){
return Leads.find();
};
Template.AddLeads.events = {
"click input.submit" : function () {
var name = document.getElementById('input#name').value();
var email = document.getElementById('#email').value();
var type = document.getElementById('#type').value();
var date = document.getElementById('#date').value();
var message = document.getElementById('#message').value();
Leads.insert({leadName : name, leadEmail : email, leadType : type, leadDate : date, leadComment : message});
}
};
} // end is_client
document.getElementById expect the id, not the selector. Also, value is a property of an input, not a function. So your input queries should be like this.
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var type = document.getElementById('type').value;
var date = document.getElementById('date').value;
var message = document.getElementById('message').value;

Categories

Resources