Unable to get data from firebase using the first array value - javascript

I am not able to get the email list data from firebase. All it shows is "null".
Although the variable agent0 shows the correct selected data from the array, I can't seem to use it with the firebase reference.
How can I use one of the array values to get data from firebase?
var agent0 = agents[0];
window.alert(agent0);
var refPullEmail = firebase.database().ref("Forwarder Contact").child("All Origins").child(agent0);
refPullEmail.once("value")
.then(function(snapshot) {
var agentEmail_0 = snapshot.child("EmailList").val(); //return list emails
window.alert(agentEmail_0);
});
Database :

To retrieve the EmailList try the following:
var refPullEmail = firebase.database().ref("Forwarder Contact").child("All Origins").child("ALLIANCE");
refPullEmail.once("value")
.then(function(snapshot) {
var agentEmail_0 = snapshot.child("EmailList").val(); //return list emails
window.alert(agentEmail_0);
});
The above will retrieve the EmailList under node ALLIANCE.
If you want to retrieve all the EmailList under node All Origins, then try the following:
var refPullEmail = firebase.database().ref("Forwarder Contact").child("All Origins");
refPullEmail.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot){
var agentEmail_0 = snapshot.child("EmailList").val(); //return list emails
window.alert(agentEmail_0);
});
});
Here, you iterate inside the direct children of All Origins and retrieve the EmailList.

Related

Accessing specific key and its value in a object returned from firebase

I have a JS function which goes into a Firebase realtime database to read some objects. The objects it checks have several fields but I only want one of these, the URL key and its value. Here is my function which retrieves the data:
const dbRefObject = firebase.database().ref('gifs');
dbRefObject.on('value', gotData);
function gotData(data){
dbRefObject.once('value', function(snapshot){
snapshot.forEach(function(childSnapshot){
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
console.log(childKey, " ", childData);
})
}
)
}
The data that is actually returned looks like this:
Content Description: "Hack The Planet"
Date Added: "08/04/2020"
Permission: "Admin"
URL: "https://firebasestorage.googleapis.com/v0/b/..."
I only want to access the URL key and its data but I cant seem to do so. I have tried doing childData[3] but it doesn't work. Any help would be appreciated.
Child values in Realtime Database are always specified by their name, not by their order. To get the URL from the snapshot, you will want to use:
var childData = childSnapshot.val();
var URL = childData.URL;
Note that the names of the children become the keys of the object returned by val().

Getting highest values from Firebase database

I am making a game in JavaScript. I want to save scores(with names of the players) in Firebase Databse. Here is the structure of my database, but it can be modified if it is needed:
.
How can I get and save to an array i.e. 3 of the best scores and belonging to them names?
Here is part of my code where I am saving the scores and trying to get them:
function writeUserData(name, score) {
firebase.database().ref("scores/").push({
name: name,
score: score,
});
}
var bestScores = {};
var scoresRef = database.ref("scores");
scoresRef.orderByValue().limitToLast(3).on("value", function(snapshot) {
snapshot.forEach(function(data) {
bestScores[data.key] = data.val();
});
});
You're ordering the nodes under scores based on the value of a child property, so in that case you need to use orderByChild and not orderByValue.
var scoresRef = database.ref("scores");
scoresRef.orderByChild("score").limitToLast(3).on("value", function(snapshot) {
snapshot.forEach(function(data) {
console.log(data.key); // "-M-...."
console.log(data.val().name); // "Anrzej"
console.log(data.val().score); // 11
});
});

Firebase Web get first Child data from autoChildID

My database looks like this:
I am trying to get the first child of unassignedBeaconIDs, read the data beaconID, major, minor and delete it from unassignedBeaconIDs.
Here is my code:
var refBeaconID = firebase.database().ref(pathToBeaconIDs).limitToFirst(1);
refBeaconID.once('value', function(snap)
{
var firstItem = snap.val(); // first item, in format {"<KEY>": "<VALUE>"}
console.log("The first item");
console.log(firstItem.beaconID);
refBeaconID.child(firstItem.key).removeValue;
});
The problem is the childAutoID Value. I always get undefined as a result.
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
So you'll need to loop over the children of the snapshot in your callback:
var refBeaconID = firebase.database().ref(pathToBeaconIDs).limitToFirst(1);
refBeaconID.once('value', function(snap)
{
snap.forEach(function(child) {
var firstItem = child.val();
console.log("The first item");
console.log(firstItem.beaconID);
});
});
I'm not completely certain what refBeaconID.child(firstItem.key).removeValue; translated to, but if you want to delete the child after logging it: child.ref.remove();.

Updating SP.ListItem with SP.Field Value from SP.PeoplePicker

Im trying to update an SP.Listitem withholding an spUser with another user with the use of JSOM. See codesnippet bellow
// Query the picker for user information.
$.fn.getUserInfo2 = function () {
var eleId = $(this).attr('id');
var siteUrl = _spPageContextInfo.siteServerRelativeUrl;
var spUsersInfo = GetPeoplePickerValues(eleId);
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('VLS-dokument');
var itemArray = [];
for(i=0;i<$.miniMenu.i.results.length;i++)
{
var item = $.miniMenu.i.results[i];
var oListItem = oList.getItemById(item.Id);
oListItem.set_item('Informationsägare', SP.FieldUserValue.fromUser(spUsersInfo.Key));
oListItem.update();
itemArray.push(oListItem);
clientContext.Load(itemArray[itemArray.Length - 1]);
}
clientContext.executeQueryAsync(Function.createDelegate(this, function () { alert(""); }), Function.createDelegate(this, function () { alert(""); }));
return spUsersInfo; //.slice(0, -2)
}
spUsersInfo contains the user obj, peoplePicker.GetAllUserInfo()
The return off SP.FieldUserValue.fromUser(spUsersInfo.Key) to be the problem since the app crash reaching that line oListItem.set_item('Informationsägare', SP.FieldUserValue.fromUser(spUsersInfo.Key));
What part of the user obj is supposed to be passed into SP.FieldUserValue.fromUser(spUsersInfo.Key) if not the key?
Is there another way to do it?
People picker columns are really just lookup columns, looking up against the site collection's user information list. You can set a lookup column either by specifying the ID of the desired item in the lookup list, or by creating a special lookup field value object (letting SharePoint do the work of finding the ID, given the desired text value).
According to the documentation the value passed to SP.FieldUserValue.fromUser() should be the user's name as a string. In practice, this should be the user's display name from the user information list.
So if you don't know the user's lookup ID, but do know their display name, you would use this: oListItem.set_item('Informationsägare',SP.FieldUserValue.fromUser(username));
If you didn't know the user name, but did know the lookup ID of the user, you could instead pass that number to item.set_item() directly, i.e. oListItem.set_item('Informationsägare',lookupId);.
If the spUsersInfo.Key value from your GetPeoplePickers() method is in the format of i:0#.w|Cool Person then you can split that value and just get the string Cool Person to feed into SP.FieldUserValue.fromUser().

How to extract data from cookie using angular ngCookie?

I have cookie data printed in console now i want to create object that will get data from cookie firstname ,lastname,eamil and id. How i can create object when i recieve data in below format ?
mainCtrl.js
angular.module('angularModelerApp')
.controller('AccessCtrl',['$scope', '$cookies','UserAccessFactory',
function ($scope, $cookies,UserAccessFactory) {
$scope.newUser = {};
$scope.patternAttuid = new RegExp("^[a-z]{2}[0-9]{3}[a-z0-9]$");
$scope.cookie = $cookies.get('attESHr');
console.log('newUser',$scope.cookie)
});
data printed $scope.cookie
newUser Mike|Pierro|mp529u#us.att.com|||sl3561||mp529u,RHCRSMK,SBGPQX9,4131585|NNNNNNNNNNNNNNYNNYNNNNNN|MIKE|EY1PE2600|
You can use split to get the values separated. You should know that doing this is not encouraged because if the format changes, your code will be broken. But if you really need to parse that string, you could try this:
var parts = $scope.cookie.split("|");
$scope.newUser.firstName = parts[0];
$scope.newUser.lastName = parts[1];
$scope.newUser.email = parts[2];
// ... etc with all the other values, keeping in mind the index

Categories