Nodejs Mysql ending - mysql

how to check if query ends in nosejd mysql
var query = connection.query(...);
i have used this and worked
query.on('end', function() {
});
but i want another way to do this like
if(query.end){
//do something
}
i know this is silly question but i need this instead of other function to do something else or otherwise i want to know how to call query on end

Related

Is there a better way to get data from localStorage after storing the data without refreshing/reloading the page

I'm trying to make a button that could store the data to the localStorage. After storing the data, I want to get the data without refreshing/reloading the page
The way I use to fix this case is, I put 2 commands on a button. The first command is to store the data, and the second command is to get the data using $scope to make it easy to display it on the page
Here is my code
$scope.storeData = function(){
if(localStorage.getItem('value') === null){
// The value that will set to the localStorage
$scope.data = 'Selamat sore';
// To set the value on $scope.data to localStorage
localStorage.setItem('value', JSON.stringify($scope.data));
//To get the value and display it on the page
$scope.getData = JSON.parse(localStorage.getItem('value'));
}else{
$scope.getData = JSON.parse(localStorage.getItem('value'));
}
}
It's actually working, but maybe there is a better way to do this
Thanks
I find this cleaner, and simpler.
function MyController($scope) {
// if value is null a default 'Selamat sore' is set
$scope.data = localStorage.getItem('value') || 'Selamat sore';
$scope.saveData = function() {
localStorage.setItem('value', JSON.stringify($scope.data));
}
}

Have to use manual wait() to get CodeceptJS/Puppeteer custom helper to see table(td tr)

I have a codeceptjs/puppeteer project and am building a custom helper for accessing information in tables. I have been able to make this work, but only by putting a two second wait in my test step before calling on the async function in my custom helper class. Given that this is all based on async/await, I have to believe I am just missing something and there is a clean way to do this.
Function from my helper class.
async getRowCount() {
//const browser = this.helpers['Puppeteer'].browser;
const page = this.helpers['Puppeteer'].page;
page.waitForSelector('tbody');
let rowCount = await page.$$eval('tbody tr', rows => rows.length);
return rowCount;
// These work
// page.waitForSelector('a[href="#/site/create"]');
// page.click('a[href="#/site/create"]');
}
My codeceptjs scenario is below.
Scenario.only('Table check ALL', async (I, loginAs) => {
loginAs('bob');
I.say(await I.getRowCount());
I.wait(3);
});
When the code is as shown above, my row count that is returned in always 0.
However, if I put a 1 second wait just before the I.getRowCount() function, then the correct total number of rows for the tbody tr selector is returned.
If anyone can help me understand why this is happening and what I can do to fix it so I don't have to pepper my code with manual wait steps to accommodate these helper functions (core "feature" of codeceptjs), I would greatly appreciate it.
Thank you!
You need to await waitForSelector:
await page.waitForSelector('tbody');
Almost all page methods are returning promises, so you have to await them.

chrome.idle: how can I query the current detection interval?

You can do this:
chrome.idle.setDetectionInterval(60*60);
chrome.idle.onStateChanged.addListener(my_code)
However, someone on the browser console can then type in:
chrome.idle.setDetectionInterval(15);
...and this will affect the way that my_code is called. This is useful for debugging, but it begs the question: how can I query the current detection interval?
There is no way (at least none that I or Google know of) to query the detection interval. However we know that the default is 60 seconds, and it's possible to override the function. (You just need to make sure that nobody calls setDetectionInveral in your app's environment before you overrode the function - and if you can't be sure, you would have to manually set the detection interval to a known value afterwards.)
Example:
var currentDetectionInterval = 60;
var originalSetDetectionInterval = chrome.idle.setDetectionInterval;
chrome.idle.setDetectionInterval = function(detectionIntervalInSeconds, callback) {
currentDetectionInterval = detectionIntervalInSeconds;
return originalSetDetectionInterval.apply(this, arguments);
};
// Optional, if you like this.
chrome.idle.getDetectionInterval = function() {
return currentDetectionInterval;
};
This would allow you to query the detection interval by checking the variable currentDetectionInterval, or if you like this better, by using the new function chrome.idle.getDetectionInterval we just added.

Show different default page depending on stored value

I am trying to make an windows store app where the default page (first page that comes up when app loads) changes depending on stored value.
I have following files
- js
|- default.js
- default.html
- page_A.html
- page_B.html
default.js has the following code:
if (localStorage["value"] == undefined || localStorage["value"] == "pageA") {
localStorage["value"] = "pageA";
//WinJS.Navigation.navigate("page_A.html");
window.location.assign = "page_A.html";
} else {
localStorage["value"] = "pageB";
//WinJS.Navigation.navigate("page_B.html");
window.location.assign = "page_B.html";
}
WinJS.Navigation code does not work at all. So I tried using window.location and what's happening is instead of loading the actual page, it loads an empty page as shown below.
I tried using both href and assign for windows.location object. What's interesting is that it seems like href and assign loads the page because if I have page_A/B.js associated with pageA/B.html and have a simple console.log statement, then the log statement does get logged, but it does not render the page.
Any ideas? I've been stuck for a while.
Try putting your default.js at the root of your project, next to page_A.html and page_B.html, or, and I don't know if this works, you can try calling those pages with ..\page_X.html.
Also, you can add an error handler function to your navigate in case there's something else going on that you're not seeing.
WinJS.Navigation.navigate('page_A.html', {}).then(function () {
// it worked!
}, function (err){
// something went wrong
});

two functions for one onsubmit

I am trying to get two function to work on one onsubmit, I made this code:
onsubmit="return validateForm1(); return validateForm2()">
but this makes validateForm1() function just work and validateForm2() doesn't ,
how to make both functions work.
You can't return 2 times in a row. First return will complete the logic of your function.
Instead you should use something like this:
var myElement = document.getElementById("myElement");
myElement.onsubmit = function(){
validateForm1();
validateForm2();
}
Also you shouldn't use inline javascript the way you do this right now. I think you might find this article useful.
You can't have to functions returning at this point. You could use the result of both in a combined call:
function validateBoth() {
return validateForm1() && validateForm2();
}