Firebase querying data limitToLast - firebase

I am still working with previous API, however I just came across one little issue. I am trying to query data from Firebase based on the limitToLast(100), however the .val() of snapshot is always null.
Here is sample data structure of my Messaging:
“Messaging” :{
"10153530036627802" : {
"10155174395220402" : {
"LastUpdate" : 1429295238531,
"Messages" : {
"1428338024339" : {
"date" : 1428338024339,
"message" : "Yo",
"sender" : "Uksz"
},
"1428338024488" : {
"date" : 1428338024487,
"message" : "Yo",
"sender" : "Uksz"
},
"1429023827304" : {
"date" : 1429023827303,
"message" : "Fygh",
"sender" : "Uksz"
},
"1429023828469" : {
"date" : 1429023828468,
"message" : "Fygh",
"sender" : "Uksz"
},
"1429295238532" : {
"date" : 1429295238531,
"message" : "Hgh",
"sender" : "Uksz"
}
},
"Name" : "Matt",
"Picture" : "https://scontent.xx.fbcdn.net/xxxx"
},
NEXT_USER_ID
And my code is:
var firebaseRef = new Firebase ("https://xxxx.firebaseio.com/Messages/");
var messageRef = firebaseRef.child(userId).child(recipient).child("Messages");
var messagesRef2 = messageRef.limitToLast(10);
messagesRef2.on("value", function(snapshot){
console.log(snapshot.val())
})
Whenever I am getting snapshot.val(), its value is equal to null. Where did I go wrong?
Thanks,
Uksz

Related

Wiremock matchesJSONPath if null or empty

I'm trying to add a Wiremock stub that matches if the JSON in a request body is either non-existent OR an empty string.
The stub I have at the moment is:
{
"id" : "e331007e-3e6d-4660-b575-b04e774e88c6",
"request" : {
"urlPathPattern" : "/premises/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/bookings/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/non-arrivals",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.[?(#.reason === '' || #.reason == null)]"
} ]
},
"response" : {
"status" : 400,
"jsonBody" : {
"type" : "https://example.net/validation-error",
"title" : "Invalid request parameters",
"code" : 400,
"invalid-params" : [ {
"propertyName" : "reason",
"errorType" : "blank"
} ]
},
"headers" : {
"Content-Type" : "application/problem+json;charset=UTF-8"
}
},
"uuid" : "e331007e-3e6d-4660-b575-b04e774e88c6"
}
It matches is the reason is '', but not if reason is not present. Any ideas?

Flutter Firebase Cloud Functions deleting all items as soon as they are written

I'm trying to use the deleteOldItems Cloud Functions code for my project, when I use the code and simply change the 2 hours to 4 it deletes all my nodes as soon as they are written. Here is my Firebase functions code...
exports.deleteOldItems = functions.database.ref('messages/{pushId}').onWrite(async (change) => {
var ref = change.after.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 4 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value', function(snapshot) {
// create a map with all children that need to be removed
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});
And here is how I'm saving my timestamp in Flutter to
database/messages
with child 'timestamp'..
var time = DateTime.now().millisecondsSinceEpoch;
Here is my JSON # 'messages'
"messages" : {
"-M3e5Glv5JEaz12hDwBB" : {
"body" : "Hello",
"name" : "Charles",
"photo" : "https://media1.giphy.com/media/7wDEaTbCupuRq/giphy-downsized.gif",
"timeStamp" : 1585549220985,
"user" : "4eb8920a-e407-4488-bce4-c6f64f7b0891"
},
"-M3e5Jfx2cnbtMTNLPev" : {
"body" : "Example",
"name" : "Charles",
"photo" : "https://media1.giphy.com/media/7wDEaTbCupuRq/giphy-downsized.gif",
"timeStamp" : 1585549232893,
"user" : "4eb8920a-e407-4488-bce4-c6f64f7b0891"
},
"-M3e6oYk6_zTe6IAs0Fg" : {
"bodyGif" : "https://media3.giphy.com/media/8xomIW1DRelmo/giphy.gif",
"name" : "Charles",
"photo" : "https://media1.giphy.com/media/7wDEaTbCupuRq/giphy-downsized.gif",
"timestamp" : 1585549625584,
"user" : "4eb8920a-e407-4488-bce4-c6f64f7b0891"
},
"-M3hR_DrGcKhM8zpFbYA" : {
"body" : "Again",
"name" : "Charles",
"photo" : "https://media1.giphy.com/media/7wDEaTbCupuRq/giphy-downsized.gif",
"timeStamp" : 1585605399479,
"user" : "4eb8920a-e407-4488-bce4-c6f64f7b0891"
}
and my database security rules are as follows for 'messages'...
"messages": {
".indexOn": ["timestamp"]
}
How can I get the nodes to be removed as expected?

Get profile information retrieved from sign in users firebase null return

From my firebase data I`m trying to access the users information like what government agency and displayname using this example from firebase docs. When I use this example came f
{
"users" : {
"citizen" : {
"NiPS4VtdFPgVMVyhCVfMtzdP85g1" : {
"displayName" : "user-citizen",
"email" : "user#user.com",
"password" : "palls123",
"pnumber" : "123",
"repassword" : "palls123"
},
"Pba7iULL4wYkTQxHB5tpbNDj7v02" : {
"displayName" : "kevin",
"email" : "ez#ez.com",
"password" : "palls123",
"pnumber" : "545",
"repassword" : "palls123"
},
"gf2o8dhs9VhIsCK5Ui5rEizESbj1" : {
"displayName" : "kevin",
"email" : "dli#dli.com",
"password" : "palls123",
"pnumber" : "5454",
"repassword" : "palls123"
}
},
"government" : {
"bfp" : {
"AgIqgADtMeNS5d3BJTnCI0uCjcl1" : {
"displayName" : "bfp-user",
"email" : "bfp#bfp.com",
"gov_agency" : "bfp",
"gov_id" : "123",
"password" : "palls123",
"pnumber" : "1234",
"repassword" : "palls123",
"toggle" : true
}
},
"ndrmmc" : {
"XdoC2Dny5ZM69z8B2BrSWBS3CIY2" : {
"displayName" : "ndrmmc-user",
"email" : "ndrmmc#ndrmmc.com",
"gov_agency" : "ndrmmc",
"gov_id" : "123",
"password" : "palls123",
"pnumber" : "123",
"repassword" : "palls123",
"toggle" : true
}
},
"pnp" : {
"MhKiEoCtytQAHooHclSZIB3gsK42" : {
"displayName" : "pnp-user",
"email" : "pnp#pnp.com",
"gov_agency" : "pnp",
"gov_id" : "23455",
"password" : "palls123",
"pnumber" : "1234",
"repassword" : "palls123",
"toggle" : true
}
},
"rta" : {
"fehvPVwX6NXnVkf3hOyRsQBDH2Y2" : {
"displayName" : "rta-user",
"email" : "rta#rta.com",
"gov_agency" : "rta",
"gov_id" : "1234",
"password" : "palls123",
"pnumber" : "123",
"repassword" : "palls123",
"toggle" : true
}
}
}
}
I`m trying to access the users information like what government agency and displayname using this example from firebase docs. When I use this example came from the docs
var user = firebase.auth().currentUser;
if (user != null) {
user.providerData.forEach(function (profile) {
console.log(" Gov Agency : " + profile.gov_agency);
console.log(" Provider-specific UID: " + profile.uid);
console.log(" Name: " + profile.displayName);
});
}
It returns null on my side except for the uid it gives email of the user. Where did I go wrong?
PS: There are two types of user a gov client and citizen client
It is normal that you get a null value for profile.gov_agency: as detailed here, the providerData property of a User is a "non-null Array of firebase.UserInfo"
In turns, a UserInfo has a set of defined properties: email, phoneNumber, etc. So the profile does not have any gov_agency property.
You are apparently mixing up the values you can get from firebase.auth with the ones that you have saved in the real time database.
You should therefore query the database, based on the userId or on the email (depends on your data model), in order to get the values of gov_agency (and displayName) to "determine if it is a gov client or a citizen client".
To query the database, refer to https://firebase.google.com/docs/database/web/read-and-write and https://firebase.google.com/docs/database/web/lists-of-data
Note that displayName IS also property of UserInfo, but apparently you have not set it when creating the users.
Note from the doc:
currentUser might also be null because the auth object has not
finished initializing...
Try:
firebase.auth().onAuthStateChanged(user => {
if (user) {
// User is signed in.
console.log('User is signed in:', user);
} else {
// No user is signed in.
console.log('No user is signed in', user);
}
});
CMIIW

Loading mapbox with Firebase database

I'm trying to learn Firebase and Mapbox and wanted to integrate the two. Firebase stores some of my data in the following format:
{
"messages" : {
"-KUE2EwfvbI48Azw01Hv" : {
"geometry" : {
"coordinates" : [ 28.6618976, 77.22739580000007 ],
"type" : "Point"
},
"properties" : {
"description" : "xyz",
"hashtag" : "#xyz",
"imageUrl" : "xyz.jpg",
"name" : "Xyz Xyz",
"photoUrl" : "xyz.jpg",
"title" : "XYZ"
},
"type" : "Issue"
},
"-KUD2EwfvbI48Azw01Hv" : {
"geometry" : {
"coordinates" : [ 12.9715987, 77.59456269999998 ],
"type" : "Point"
},
"properties" : {
"description" : "xyz",
"hashtag" : "#xyz",
"imageUrl" : "xyz.jpg",
"name" : "Xyz Xyz",
"photoUrl" : "xyz.jpg",
"title" : "XYZ"
},
"type" : "Issue"
}
}
}
Is there a way to load the data and plot it into Mapbox? The examples require a GeoJSON file hosted somewhere that can be used to plot them. How can we use the Firebase database to plot on the Mapbox in realtime?
Sorry if my question is ambiguous. I'm willing to provide more information if needed :D
Thanks!
You can load the data, but you first have to convert it to a valid GeoJSON object.
Here is a JSFiddle using the data you provided:
https://jsfiddle.net/mkrv9uuy/
var firebaseGeojsonFeatures = [];
for (var key in firebaseData.messages) {
var f = firebaseData.messages[key];
f.type = "Feature";
firebaseGeojsonFeatures.push(f);
}

Client does not have permission to access the desired Firebase data

I have a page that is calling addCheckin() method which is inside a controller. In the controller, I am trying to create a reference as follows:
var ref = firebase.database().ref("users/" + $scope.whichuser + "/meetings/" +$scope.whichmeeting + "/checkins");
$scope.whichuser and $scope.whichmeeting are the $routeParams that I am passing from another route.
Here's my checkin controller-
myApp.controller("CheckinsController",
['$scope','$rootScope','$firebaseArray','$routeParams','$firebaseObject',
function($scope,$rootScope,$firebaseArray,$routeParams,$firebaseObject){
$scope.whichuser = $routeParams.uid;
$scope.whichmeeting = $routeParams.mid;
var ref = firebase.database().ref("users/" + $scope.whichuser + "/meetings/" +$scope.whichmeeting + "/checkins");
$scope.addCheckin = function(){
var checkinInfo = $firebaseArray(ref);
var data={
firstname:$scope.firstname,
lastname:$scope.lastname,
email:$scope.email,
date:firebase.database.ServerValue.TIMESTAMP
}
checkinInfo.$add(data);
}
}]);/*controller*/
There are two errors that I am getting here-
Error 1:
Error: permission_denied at /users/Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3/meetings: Client doesn't have permission to access the desired data.
Error 2:
Error: permission_denied at /users/Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3/meetings/-KT5tqMYKXsFssmcRLm6/checkins: Client doesn't have permission to access the desired data.
And this is what I am trying to achieve:
Here's my JSON tree of the database:
{
"users" : {
"43ZU72ndoKb9LSEjBODKXe3nrou1" : {
"email" : "roy#123.com",
"firstname" : "Roy",
"lastname" : "Marshal",
"regUser" : "43ZU72ndoKb9LSEjBODKXe3nrou1"
},
"BHcYb40FC8YlCKL54ngyMhxPx2q1" : {
"date" : 1475574109981,
"email" : "sky#on.com",
"firstname" : "Sky",
"lastname" : "Thakur",
"meetings" : {
"-KTE3_45wXBqd-_yR1xv" : {
"date" : 1475574125198,
"name" : "ARET"
}
},
"regUser" : "BHcYb40FC8YlCKL54ngyMhxPx2q1"
},
"Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3" : {
"email" : "jon#me.com",
"firstname" : "John",
"lastname" : "Cena",
"meetings" : {
"-KT5tqMYKXsFssmcRLm6" : {
"date" : 1475437094667,
"name" : "hourly"
},
"-KTAvH-ZkWyxrJrK_SDb" : {
"date" : 1475521356544,
"name" : "Hourly"
},
"-KTAwV69txdeT4i_horE" : {
"date" : 1475521676006,
"name" : "asdfg"
},
"-KTAwkH35EyPQZ17DmN9" : {
"date" : 1475521742244,
"name" : "qwerty"
},
"-KTAwsrqe24e8wFfRxpN" : {
"date" : 1475521777422,
"name" : "asfdsf"
},
"-KTAyYPSZLQUtOJ4g8Ak" : {
"date" : 1475522213781,
"name" : "Yearly"
},
"-KTB05PfXZLLk9Of3_Nb" : {
"date" : 1475522881473,
"name" : "Meet"
},
"-KTBG2cP9YLoP4lalN5M" : {
"date" : 1475527064378,
"name" : "JOI"
}
},
"regUser" : "Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3"
}
}
}
My database rules are:
{
"rules": {
".read": true,
".write": true
}
}
I also posted this question before, but didn't get any appropriate solution.

Resources