Wednesday, 26 January 2022

Vue.js - How to use Set VUEX values in main.js

 The answer to 'How to use Set VUEX values in main.js' is that you don't ! 


Another niche note from myself for a Google search term that I made that this blog would have save me a bit of time chasing around the houses. 


I highly recommend the YouTube video for solving this and other Vuex issues - https://www.youtube.com/watch?v=oxUyIzDbZts&t=629s


The issue I had to solve was that I'd written some code where I was using 'localStorage' when the value should be stored in VUEX .  This was being done in the main.js page. 



firebase.auth().onAuthStateChanged(async function (user) {
console.log('Auth State Checked')
if (user) {
user.getIdTokenResult().then(idTokenResult => {
if (idTokenResult.claims.admin === true) {
localStorage.adminClaim = true
}
});
}
})


The main thing here is, we can't use Vuex from the main.js page and you can't 'commit' to Vuex from here.  That will done in a VUEX 'action'.  

In store/index.js

export default new Vuex.Store({
state: {
adminRole: false,
},
mutations: {
//syncrous
setAdminRole(state, payload) {
state.adminRole = payload;
}
},
actions: {
//asyncronous
async setAdminRole(state) {
firebase.auth().onAuthStateChanged(async function (user) {
console.log('Auth State Checked')
if (user) {
user.getIdTokenResult().then(idTokenResult => {
console.log('IdTokenResult for user', idTokenResult);
console.log('idTokenResult.claims.admin',idTokenResult.claims.admin);
if (idTokenResult.claims.admin === true) {
state.commit("setAdminRole", true);
}
});
}
})
}
},
modules: {},
getters: {
getAdminRole: state => state.adminRole,
},
});


And then in App.vue we can Dispatch our 'action' like this. 

export default {
mounted(){
this.$store.dispatch("setAdminRole");
}
};


Next up is to call it in the page you want to access that data.  In our case the TopHeader page. 


Note in the video above at 8.40, he talks about timing issues.  Which is why mounted doesn’t work and we need to use computed. 



export default {
data() {
return {
},
name: "top-header",
mounted() {
},
computed: {
adminRole(){
return this.$store.getters.getAdminRole;
},
}
};


This will load a data variable 'adminRole' with our Boolean task.  So using 

{{ adminRole }}

in the template, would output either True or False. 







Tuesday, 25 January 2022

Firebase Functions Claims setting one makes my other 'claims' undefined !

In my app we have 2 roles that we need to add to the ‘claims’ .

These are

  1. Admin

  2. Coordinator

Which in my Cloud Function I set the claim to either 'true' or 'false' . Which is 'Add' and 'Remove' respectively.


However I was noticing that when setting one it was making the other claim 'undefined' . Here's what one of my functions looked like. exports.addAdminRole = functions.https.onCall((data, context) => { 3 return admin.auth().getUserByEmail(data.email).then(user => { 4 return admin.auth().setCustomUserClaims(user.uid, { 5 admin: true, 7 }); 8 }).then(() => { 10 return { 11 message: `Success! ${data.email} has been made an admin` 12 } 13 }).catch(err => { 14 return err; 15 }) 16})

To fix this issue I had to pass though instructions on what the other Roles claim should be.
I'm using Vue.js by the way. So, the other role is set in the 'data'. Which I can pick up and pass it through like this
removeAdminRole({email: this.email, coordinator_role: this.coordinator_role}).then(() => {
this.successMessage = "The admin role has been removed !";
})
And then Cloud function will look like this.
exports.addAdminRole = functions.https.onCall((data, context) => {
return admin.auth().getUserByEmail(data.email).then(user => {
return admin.auth().setCustomUserClaims(user.uid, {
admin: true,
coordinator: data.coordinator_role
});
}).then(() => {
return {
message: `Success! ${data.email} has been made an admin`
}
}).catch(err => {
return err;
})
})


This now works as it should for me.

Tuesday, 18 January 2022

Firebase Functions - Callable Function returning 'CORS' error.

firebase function has been blocked by CORS policy 


I experienced this issue while following a tutorial by The Net Ninja - Firebase Functions


function/index.js

1exports.sayHello = functions.https.onCall((data, context) => { 2 return `Yo dudes and dudettes`; 3})


DEPLOY this to Google .

And then in our button action ( fired by javascript )

1const sayHello = firebase.functions().httpsCallable('sayHello'); 2 sayHello().then(result => { 3 console.log(result.data); 4 });

 

I’d then get a CORS issue firebase function has been blocked by CORS policy

The following call to assign the region worked for me in the Google Function itself.

exports.sayHello = functions.region('us-central1').https.onCall((data, context) => {
return `Yo dudes and dudettes`;
})


Thursday, 6 January 2022

Google Data Studio ( GDS ) - Removing 'No data' from Scorecard Display

On my Scorecards where I have single values being pulled in, it is showing the text 'No data' underneath the value.  Like this. 



To remove that it's pretty simple.  Click on 'Style' on the 'Scorecard' editor. 

You then have a dropdown for 'Missing Data' 

Here select 'Show * ( blank ) ' 

That's it :)




Wednesday, 10 November 2021

Python Panda Find and Replace a value

For the issue I was trying to solve, I knew which Column this value would appear in.  It is also the entire contents of the cell that I need to replace. 

After searching for 'python panda find and replace a value' I found this post  https://www.geeksforgeeks.org/python-pandas-dataframe-replace/ 

This suggests 

df.replace(to_replace ="Text I don't want",
                 value ="Text I do want")

However this didn't work for me.  What did work was to name the column specifically and to use 'str.replace', as mentioned here 

https://stackoverflow.com/questions/53141072/replacing-column-data-with-pandas-replace-not-working

uk['my_col'] = uk['my_col'].str.replace("Text I don't want", "Text I do want")

Wednesday, 3 November 2021

Importing .Csv data into a table in Sequel Pro.

First you need to CREATE your table. 

Now click on the Table name on the left hand site of the Sequel Pro console. 

Next, select File > Import from the top Menu bar. 


From here on you'll have a wizard that'll talk you through the column matching. 


And that's it. 



Thursday, 21 October 2021

DigitalOcean - FATAL: remaining connection slots are reserved for non-replication superuser connections.

 We were getting this error on an App running on Digital Ocean.  

This is what you'll find if you Google the issue 

https://www.digitalocean.com/community/questions/fatal-remaining-connection-slots-are-reserved-for-non-replication-superuser-connections

You’ll need to Edit the PostgreSQL configuration file and increase the value for maxconnections. The value for sharedbuffers may also need to be increased.

You may see here how more connections have been created:


This setting would be changed at →

  1. Login to DigitalOcean – The developer cloud

  2. Click on ‘Databases’

  3. Click on your DB

  4. Click on the ‘Connection Pools’ tab


This led us to page showing that we have 22 connections that we could use.  So why are we using all our connection slots on digital ocean. 

AND OUR SITUATION IT WAS THE CODE AT FAULT. 

In the code there were 13 connections to the DB being opened but they were not being closed. 
What I needed to do is add the db.dispose() after the connections been used. 


db_engine = create_engine('[CONNECTION_DETAILS]', pool_pre_ping=True)
...
db_engine.dispose()