Meteor collection insert is inserting subdocument instead of plain document. see my insert statement below:
return Profile.insert({
_id: this.userId,
"firstName": firstname,
"lastName": lastname,
"state": state,
"mobile": mobile,
"alias": alias,
"email": email,
"bvn": bvn,
"createdAt": new Date(),
"updatedAt": new Date(),
});
And this is the result in MongoDB console:
meteor:PRIMARY> db.profile.find().pretty()
{
"_id" : "uNECMJJkCtQXhSs33",
**"firstName" : {
"firstName" : "firstName",
"lastName" : "lastName",
"state" : "state",
"mobile" : 55325235522535,
"alias" : "alias",
"email" : "email",
"bvn" : 6364634664
},**
"createdAt" : ISODate("2018-12-15T03:23:33.243Z"),
"updatedAt" : ISODate("2018-12-15T03:23:33.243Z")
}
Below is my expectation
meteor:PRIMARY> db.profile.find().pretty()
{
"_id" : "uNECMJJkCtQXhSs33",
"firstName" : "firstName",
"lastName" : "lastName",
"state" : "state",
"mobile" : 55325235522535,
"alias" : "alias",
"email" : "email",
"bvn" : 6364634664
"createdAt" : ISODate("2018-12-15T03:23:33.243Z"),
"updatedAt" : ISODate("2018-12-15T03:23:33.243Z")
}
I have resolved the issues. I had to pass all the parameters as object from the template into the method as follow:
Template
const updateProfile = {
"alias":target.aliasName.value,
"email":target.email.value,
"state":target.state.value,
"mobile":target.phone.value
}
Meteor.call('profileUpdate', updateProfile);
Method :
Meteor.methods({
'profileInsert'(profile) {
if (!this.userId) {
throw new Meteor.Error('not-authorized');
}
Match.test(profile.firstname, String);
Match.test(profile.lastname, String);
Match.test(profile.state, String);
Match.test(profile.mobile, Number);
Match.test(profile.bvn, Number);
Match.test(profile.email, String);
Match.test(profile.alias, String);
return Profile.insert(profile);
} });
Related
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
For example - How to get the node -L1BchvA8NaQ9Yh7CjS_ which matches email = tomcruise#firebasemyapp.com? when the database looks like
{
"-L1BchvA8NaQ9Yh7CjS_" : {
"createdAt" : 1514187972592,
"displayName" : "tom cruise",
"email" : "tomcruise#firebasemyapp.com",
"firstName" : "tom",
"lastName" : "cruise",
"profileLink" : "https://reactjs.org/logo-og.png",
"status" : "happy christmas"
},
"-L1CcUEBOECrJJozlHJv" : {
"createdAt" : 1514204689673,
"displayName" : "Robert John Downey Jr",
"email" : "robertjohndowneyjr#firebasemyapp.com",
"firstName" : "Rober John",
"lastName" : "Downey Jr",
"profileLink" : "https://reactjs.org/logo-og.png",
"status" : "happy christmas"
},
}
Edit:-
I am using react-native.js.
You didn't mention what language you're using, but I'll answer in javascript and it shouldn't be difficult to change into a different language.
var database = firebase.database();
var rootRef = database.ref();
rootRef.orderByChild('email')
.equalTo('tomcruise#firebasemyapp.com')
.on('child_added')
.then(function(snapshot){
var result = snapshot.val();
//Do what you want with the result
});
I found solution from Common SQL Queries converted for the Firebase Database.
var currentUserUID = firebase.auth().currentUser.uid;
let dbRef = firebase.database().ref("ARUserProfiles/" + currentUserUID);
dbRef
.orderByChild("email")
.equalTo("tomcruise#firebasemyapp.com")
.once(
"value",
response => {
const val = response.val();
let responsePayload;
if (val) {
responsePayload = Object.entries(val);
console.log("ARUserProfiles :-", responsePayload);
}
},
error => {
console.log(error);
}
);
My firebase database structure is as below. I would like to search with a particular "Info/Email", let's say "abc#abc.com".
{
"-KhWrBcYyMluJbK7QpnK" : {
"Info" : {
"Email" : "xyz#gmail.com"
},
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX0tgQvDtDYqt4XRoL" : {
"Info" : {
"Email" : "abc#abc.com"
},
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX1eo7uFnOxqncDXQ5" : {
"Info" : {
"Email" : "abc#abc.com"
},
"Settings" : {
"Status" : "Pending"
}
}
}
I added a rule too
"Invitation" : {
".indexOn": ["Info/Email","Settings/Status"]
}
My AngularFire code is as follows:
var rootRef = firebase.database().ref().child('Invitation');
var userInvitations = rootRef.child("Info").orderByChild("Email").equalTo("abc#abc.com");
var allInvitations = $firebaseArray(userInvitations);
But I am getting a FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "Email" at /Invitation/Info to your security rules for better performance. and of course I am not receiving any data.
What are the mistakes I made here? Also can I add multiple orderByChild, for example: if I want to find details of the record, which has "Info/Email" equal to "abc#abc.com" and "Settings/Status" equal to "Pending" ?
The Firebase API only allows you to filter children one level deep
So with reference to your data structure:
if it were to be this way,
{
"-KhWrBcYyMluJbK7QpnK" : {
"Email" : "xyz#gmail.com",
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX0tgQvDtDYqt4XRoL" : {
"Email" : "abc#abc.com",
"Settings" : {
"Status" : "Accepted"
}
},
"-KhX1eo7uFnOxqncDXQ5" : {
"Email" : "abc#abc.com",
"Settings" : {
"Status" : "Pending"
}
}
}
You should write your rules this way instead.
"Invitation" : {
"Info" : {
".indexOn": "Email",
},
"Settings" : {
".indexOn": "Status",
}
}
Then you will be able to query the data
var allInvitations = [];
var rootRef = firebase.database().ref().child('Invitation');
var userInvitations = rootRef.orderByChild("Email").equalTo("abc#abc.com");
userInvitations.on('value', snap => {
var invitations = snap.val()
for (prop in invitations ) {
allInvitations.push(invitations[prop ]);
}
console.log(allInvitations);
})
I am using nativescript-plugin-firebase to query firebase database in my angular2-nativescript application. I went through the documentation on how to query the database by field. For example I would like to fetch address of a user, based on uid for the below example database. But I could not find a way. Any help will be appreciated.
{
"address" : {
"-KfBtEuTA43UzSFfK7kU" : {
"house_number" : "hno1",
"street" : "street1",
"city" : "city1",
"uid" : "0P3Km5i9cEd1Akg7gJfJnALUSZw2"
},
"-KfC4Myo69bTZQCzw1yz" : {
"house_number" : "hno2",
"street" : "street2",
"city" : "city2",
"uid" : "4sj3ADekxsVNf5RaAFjbLbF6x0K2"
}
}
}
The following code gave me the query result by uid.
firebase.query(result => {
console.log("query result:", JSON.stringify(result));
}, "/address", {
orderBy: {
type: firebase.QueryOrderByType.CHILD,
value: 'uid'
},
ranges: [
{
type: firebase.QueryRangeType.START_AT,
value: uidValue
},
{
type: firebase.QueryRangeType.END_AT,
value: uidValue
}
]
})
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