Getting contact.id after saving Cordova contact plugin - android

I need to get back the contact id after it is saved in order to save it to my online database. However the cordova contact.save() method does not return an id after execution.
Here is my logic:
if ($scope.contact.id === undefined) {
contact.save();
console.log("Contact ID is:", savedContact.id);
table.insert({ id: contact.id.value, firstname: name.givenName, lastname: name.familyName, homephone: phoneNumbers[0].value, mobilephone: phoneNumbers[1].value, email: emails[0].value });
}
This does not work.
Is there any way to retrieve the id for the contact without having to search the phones contact list using a phone number like this:
if ($scope.contact.id === undefined) {
contact.save();
var savedContact = navigator.contacts.find({ "phoneNumbers[0]": phoneNumbers[0].value });
console.log("Contact ID is:", savedContact.id);
table.insert({ id: contact.id.value, firstname: name.givenName, lastname: name.familyName, homephone: phoneNumbers[0].value, mobilephone: phoneNumbers[1].value, email: emails[0].value });
}
The above seems like way too much overhead. Not to mention it may not even return the correct contact as a phone number may not be unique.(If someone saves the contact twice with different information)

contact.save() can take two callbacks, success and failure. The success callback should return your newly saved contact (which would include the id.)
if ($scope.contact.id === undefined) {
contact.save(contactSuccess, contactFailure);
}
function contactSuccess(newContact) {
console.log("Contact ID is:", newContact.id);
table.insert({ id: contact.id.value, firstname: name.givenName, lastname: name.familyName, homephone: phoneNumbers[0].value, mobilephone: phoneNumbers[1].value, email: emails[0].value });
}
function contactError(err) {
//bb10 fires multiple error callbacks with empty errors
if (err) {
console.log(err);
}
}
Since it looks like you are using Angular, check out the ngCordova project. It provides some nice wrappers around some plugins that make everything a bit more readable. Here is the relevant excerpt from their contacts docs:
$cordovaContacts.save($scope.contactForm).then(function(savedContact) {
console.log("Contact ID is:", newContact.id);
table.insert({ id: contact.id.value, firstname: name.givenName, lastname: name.familyName, homephone: phoneNumbers[0].value, mobilephone: phoneNumbers[1].value, email: emails[0].value });
}, function(err) {
if (err) {
console.log(err);
}
});

Related

Remove element from the array by the index with Firebase Firestore? [duplicate]

I got this data in my document:
I want to delete index 0. How do I do this? This should do the trick I thought:
db.collection("data").document("free").updateData(["deleteme.deletemee.0" : FieldValue.delete()]) { (errr) in
print(errr)
}
But the errr prints nil, and nothing is removed. When getting the document I noticed something strange about the data when using this code:
db.collection("data").document("free").getDocument { (doc, err) in
guard let _doc = doc,
doc?.exists ?? false else{ return }
print(_doc.data())
}
This prints out:
["deleteme": {
deletemee = (
1 //this is the value for the key, but where is my key?! :(
);
}]
I cannot see the key, where is it? How to delete something at an index in Firestore? Thanks.
Array operations have finally been supported. Deletion, addition, etc. are supported via the value (not the index) now:
At the moment, there are a few bugs at the moment though as this one I encountered.
The dev blog here:
It is not currently possible to modify individual elements of an array stored in Cloud Firestore.
If you stored the data as a map (the keys dont matter) like this:
{
name: "sam",
things: {
one: "value",
two: "value"
}
}
Then you can delete individual elements like this:
// Delete the things.one data
db.collection("whatever").document("whatever").updateData([
"things.one": FieldValue.delete(),
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
Now the data will look like this:
{
name: "sam",
things: {
two: "value"
}
}
export const deleteArrayIndex = (collectionName, id, index) => {
db.collection(collectionName).doc(id).update(
{ [index]: firebase.firestore.FieldValue.delete() }
).then(function () {
console.log(index + " is deleted");
}).catch(function (error) {
console.error("Error removing document: ", error);
});
}

How to select an element in JSON array and bind it to the view in ionic 3?

I am making an app in ionic 3 and I am trying to fetch data from an url.
This is my code in data.ts and my data provider:
getVehicleStatus() {
return this.http.get<VehicleStatus>(this.url);
}
This is my vehicle class:
class VehicleStatus {
status: string;
plate: string;
code: string;
message: string;
}
I am calling the method into my home.ts file.
ionViewDidLoad() {
console.log(this.getStatus('test3'));
}
getStatus(plate: string) {
this.dataService.getVehicleStatus()
.subscribe((data: VehicleStatus) => this.vehiclestatus = [{ ...data }]);
}
To test out if everything works I hard code a license plate number to log it into my chrome developer tools. It said 'Undefined'.
This is how the json data looks like:
[{"status":"P","plate":"test2","code:"MGP150151","message":"fail"}
,{"status":"P","plate":"test3","code":"MGP160298","message":"fail"}
,{"status":"P","plate":"test4","code":"MGP140085","message":"succes"}
,{"status":"O","plate":"test5","code":"MGP150175","message":"succes"}]
I should get this object back:
{"status":"P","plate":"test3","code":"MGP160298","message":"fail"}
But it doesn't work and got the message undefined.
I have used the following source:
https://angular.io/guide/http
How can I search in the array and bind it to my HTML page in ionic 3?.
Can someone point me in the right direction?.
Kind regards .
The responsibility to find the VehicleStatus from the list should be of the service rather than of the Component itself.
Consider changing your Service Implementation to take up that responsibility. You can use the map operator to transform the response to return the VehicleStatus found based on the plate that will be passed as an arg while calling the getVehicleStatus method.
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
getVehicleStatus(plate): Observable<VehicleStatus> {
return this.http.get<VehicleStatus[]>(this.url)
.pipe(
map(statuses => statuses.find(status => status.plate === plate))
);
}
Then in your home.ts:
ionViewDidLoad() {
this.getStatus('test3');
}
getStatus(plate: string) {
this.dataService.getVehicleStatus(plate)
.subscribe((data: VehicleStatus) => this.vehiclestatus = data);
}
You need array.find to get the matching value, which will return the first matching element from the array
this.vehiclestatus = data.find(vehicle=>vehicle.plate === plate);

React native firebase creating user then adding additional data to realtime database

I am creating an app that allows users to register themselves.
Using react-native and firebase works, the are registered and are logged in. Even the additional data is stored in the realtime database (I checked it).
But I am getting a message saying "creating user failed" (it is one of the messages I have setup).
This is the code I use to create users and add additional data to the realtime database:
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((user) => {
firebase.database().ref('users/' + user.uid).set({
firstName: firstName,
lastName: lastName,
email: email,
code: code
})
})
.then(user => loginUserSuccess(dispatch, user))
.catch((error) => {
createUserFail(dispatch)
console.log(error);
});
I also checked the debugger in android studio (don't know about ios yet) and saw the following comment:
TypeError: undefined is not an object (evaluating 'user.uid')
Still everything is saved, but the error message was shown. What am I doing wrong?
I know this is a year old but I ran into the same issue and here's a more direct solution for anyone running into the same issue. When you create a user the user object is a part of the Firebase response not the response itself. So just prepend "res" to your original code like this:
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((res) => {
firebase.database().ref('users/' + res.user.uid).set({
firstName: firstName,
lastName: lastName,
email: email,
code: code
})
})
Ok, finally I got it working. What I did was, after the first then, checking for the currentuser (because the user will be loggedin automatically), then getting the uid and using that:
if (firebase.auth().currentUser) {
userId = firebase.auth().currentUser.uid;
if (userId) {
firebase.database().ref('users/' + userId).set({
firstName: firstName,
lastName: lastName,
email: email,
code: code
})
}
}
This works.
firebase.auth().createUserWithEmailAndPassword(email, password).then((user)=>{
if (firebase.auth().currentUser) {
userId = firebase.auth().currentUser.uid;
if (userId) {
firebase.database().ref('users/' + userId).set({
firstname:firstname,
lastname:lastname,
email:email,
password:password,
town:town,
addInterest:addInterest,
photoUrl:false,
emailVerified:false,
uid:userId,
status:true,
online:true
})
}
}
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log('Register!');
console.log(error);
})
Full Code so easy to use. 100% work!
You didn't say if you were using this in the context of an action creator but if you did, here is another possible solution. Let's make it more interesting, its an app for adding new employees:
export const employeeCreate = ({ name, phone, shift }) => {
const { currentUser } = firebase.auth();
return () => {
firebase
.database()
.ref(`/users/${currentUser.uid}/employees`)
// add a .then() to ensure after employee is created
// we want to navigate them back to employee list screen
.push({ name, phone, shift })
.then(() => Actions.pop());
};
};
The Actions would be imported from react-native-router-flux like so:
import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';

Get IMEI of an android phone on page load

I am developing an application in Cordova which requires to get the IMEI number of any device programatically. I want want get the IMEI on page load and store it on local storage
$(function(){
$('#LoginForm').submit(function(){
var loginData = $ ("#LoginForm").serialize();
$.ajax({
type: 'post',
url: "http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
data: loginData,
crossDomian: true
}).done(function (data) {
console.log(data);
alert(data.cMessage.code)
if(data.cMessage.code == "0"){
window.location.href = "Home.html"
}
else{
window.location.href = "Payment.html"
}
}).error(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseText || textStatus);
});
return false;});
})
If you're looking for a unique number to identify the device, then you can use this:
android.os.Build.SERIAL;
You can simply use the cordova plugin device to optain the unique identifier of any cordova supported OS.
//Get the device's Universally Unique Identifier
var string = device.uuid;

Authentication with Passport Session on Android

I have a web app currently running with NodeJS and Express, where I authenticate the users using Passport sessions, and it works perfectly. Here is the overview of what I do:
app.use(session({
secret : 'hidden of course :)',
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
/****** Passport functions ******/
passport.serializeUser(function (user, done) {
done(null, user.idUser);
});
passport.deserializeUser(function (id, done) {
db.user.findOne( { where : { idUser : id } }).then(function (user, err) {
done(null, user);
});
});
//Facebook
passport.use(new FacebookStrategy({
//Information stored on config/auth.js
clientID: *******,
clientSecret: ******,
callbackURL: *******,
profileFields: ['id', 'emails', 'displayName', 'name', 'gender', 'picture.type(large)']
}, function (accessToken, refreshToken, profile, done) {
//Using next tick to take advantage of async properties
process.nextTick(function () {
db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
if(err) {
return done(err);
}
if(user) {
return done(null, user);
} else {
// Check whether the email is undefined or valid
var emailTemp = '';
if(profile.emails && profile.emails[0] && profile.emails[0].value) {
emailTemp = profile.emails[0].value;
} else {
emailTemp = '';
}
var picture = '';
if(profile.photos && profile.photos[0] && profile.photos[0].value) {
picture = profile.photos[0].value;
} else {
picture = '/img/profile.png';
}
var sexFb = '';
if(profile.gender) {
sexFb = profile.gender;
} else {
sexFb = '';
}
// Create the user
db.user.create({
idUser : profile.id,
token : accessToken,
picture : picture,
nameUser : profile.displayName,
email : emailTemp,
sex : sexFb
}).then(function () {
db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
if(user) {
return done(null, user);
} else {
return done(err);
}
});
});
}
});
});
}));
app.use(express.static(__dirname + '/public/'));
/* FACEBOOK STRATEGY */
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback//
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email']}));
/* FACEBOOK STRATEGY */
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/' }),
function (req, res) {
// Successful authentication, redirect home.
res.redirect('../../app.html');
});
Now, I'm building our Android App and I need to authenticate our users, preferably using the backend I already built for the web version. I was checking some questions on SO like this one and I understood a lot of what I would have to do.
Currently my clients stay logged in through the cookie that Express-session handles, saving the user's id on the req.user, so that I can run queries like on this example:
app.put('/profile', function (req, res) {
//Updates the profile information of the user
db.user.update({
nameUser : req.body.nameUser
}, {
where : {
idUser : req.user.idUser
}
}).then(function (user) {
res.json({ yes : "yes" });
});
});
So my questions:
Can I authenticate my users using the same strategy as the one I currently have? If not, what would I have to change to be able to authenticate my users on Android?
Once they are authenticated, how can I set the req.user from Android (through HTTP request or whatever mean) to correctly perform the tasks on the backend (since all my requests are based on req.user)? I assume Android doesn't keep cookies like browsers do, so how would Passport Sessions know which user is calling the API?
I currently use Retrofit2 on my Android app to interact with the API, is it able to perform the actions necessary for this task?
Sorry for the long post, added a good chunk of code just to be on the safe side, if you guys have any questions or need any explanation please let me know!

Categories

Resources