Ionic storage GET - angularjs

I try get value from ionic storage, but it's doesn't work here. Why GET2 execute before storage.get ? My brain is broken, help please.
public storageGet(key: string){
var uid = 0;
this.storage.get(key).then((val) => {
console.log('GET1: ' + key + ': ' + val);
if (val != null) { uid = val;}
});
console.log('GET2: ' + key + ': ' + uid);
return uid;
}
Return:
GET2: uid: 0
GET1: uid: 1

You need to understand how promise works.
This code is asynchronous, all lines in the then callback will be execute, but you can't decide when.
The console.log("GET2") is executed strictly after the storage.get, this part is synchronous.

Related

jsforce metadata.deploy, deployment id

I am looking to get the created deployment id returned in the callback, how do I get it immediately when it is created ?
This is from the jsforce documentation. Here it is called only when it is completed.
var fs = require('fs');
var zipStream = fs.createReadStream("./path/to/MyPackage.zip");
conn.metadata.deploy(zipStream, { runTests: [ 'MyApexTriggerTest' ] })
.complete(function(err, result) {
if (err) { console.error(err); }
console.log('done ? :' + result.done);
console.log('success ? : ' + result.true);
console.log('state : ' + result.state);
console.log('component errors: ' + result.numberComponentErrors);
console.log('components deployed: ' + result.numberComponentsDeployed);
console.log('tests completed: ' + result.numberTestsCompleted);
});
This was dead simple, I was able to figure out this, just use the callback without the complete.

Angular 7 and Objects search how to find strict values

I am searching for a value of yes in the field hhead of an object returned from server:
Object.keys(this.data).forEach(key => {
if (this.data[key].hhead === 'yes') {
console.log('Yes '+(this.data[key].hhead === 'yes'))
this.snackBar.open('This household already have ' + this.data[key].far + ' ' + this.data[key].lar + ' (id: ' + this.data[key].iid + ' ) as a head of household', 'Close', {
panelClass: 'error'
});
}
else {
console.log('No '+(this.data[key].hhead === 'no'))
if (data['age'] <= 17 && data['age'] < this.maxAge && (selectedFr == "Head Of Household")) {
let message = 'This individual is not the oldest in his family to be the head of household. Do you want to complete this action ?';
this.openDialog(message, updateType, ind_id, newSts, newMs, newFr, newHH, oldHH, missingData);
}
}
});
The problem with this script is that both if and else are true. So both scripts will run.
The reason is that, at the first condition, once it finds a yes value, the condition turn true.
And the second, once it finds no it will run.
The array is like:
So what I need is if an array only contains no in all rows, to run the else part. And if it found at least yes to run the first condition.
I think you are trying to attack the problem from the wrong angle. You have to scan the collection first and then run your code:
var mached = this.data.every(t => t.hhead == 'yes'); //this will print true
Object.keys(this.data).forEach(key => {
if (mached) {
console.log('Yes '+(this.data[key].hhead === 'yes'))
this.snackBar.open('This household already have ' + this.data[key].far + ' ' + this.data[key].lar + ' (id: ' + this.data[key].iid + ' ) as a head of household', 'Close', {
panelClass: 'error'
});
} else {
console.log('No '+(this.data[key].hhead === 'no'))
if (data['age'] <= 17 && data['age'] < this.maxAge && (selectedFr == "Head Of Household")) {
let message = 'This individual is not the oldest in his family to be the head of household. Do you want to complete this action ?';
this.openDialog(message, updateType, ind_id, newSts, newMs, newFr, newHH, oldHH, missingData);
}
}
});

Angular unit testing with ngMock - $timeout.flush() throws exception

I am using ngMock for unit testing and I need to use the $timeout.flush function in one of my tests, so I have added the two following lines to my test:
$timeout.flush();
$timeout.verifyNoPendingTasks();
as indicated on http://www.bradoncode.com/blog/2015/06/11/unit-testing-code-that-uses-timeout-angularjs/.
$timeout.flush() does flush the timeout as expected, however I am now getting an exception from angular-mocks.js every time I run my test:
LOG: 'Exception: ', Error{line: 1441, sourceURL: 'http://localhost:9876/base/node_modules/angular-mocks/angular-mocks.js?05a191adf8b7e3cfae1806d65efdbdb00a1742dd', stack: '$httpBackend#http://localhost:9876/base/node_modules/angular-mocks/angular-mocks.js?05a191adf8b7e3cfae1806d65efdbdb00a1742dd:1441:90
....
global code#http://localhost:9876/context.html:336:28'}, 'Cause: ', undefined
Does anyone know where this exception could come from? I get it as many times as I use the $timeout.flush() function.
Looking at the angular-mocks.js file, it looks like it comes from the $httpBackend function. I have tried to update the ngMock version but it does not change anything. I have tried version 1.4.7 (which is my angular version) and version 1.6.2.
function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) {
var xhr = new MockXhr(),
expectation = expectations[0],
wasExpected = false;
xhr.$$events = eventHandlers;
xhr.upload.$$events = uploadEventHandlers;
function prettyPrint(data) {
return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
? data
: angular.toJson(data);
}
function wrapResponse(wrapped) {
if (!$browser && timeout) {
if (timeout.then) {
timeout.then(handleTimeout);
} else {
$timeout(handleTimeout, timeout);
}
}
return handleResponse;
function handleResponse() {
var response = wrapped.response(method, url, data, headers, wrapped.params(url));
xhr.$$respHeaders = response[2];
callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
copy(response[3] || ''));
}
function handleTimeout() {
for (var i = 0, ii = responses.length; i < ii; i++) {
if (responses[i] === handleResponse) {
responses.splice(i, 1);
callback(-1, undefined, '');
break;
}
}
}
}
if (expectation && expectation.match(method, url)) {
if (!expectation.matchData(data)) {
throw new Error('Expected ' + expectation + ' with different data\n' +
'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
}
if (!expectation.matchHeaders(headers)) {
throw new Error('Expected ' + expectation + ' with different headers\n' +
'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
prettyPrint(headers));
}
expectations.shift();
if (expectation.response) {
responses.push(wrapResponse(expectation));
return;
}
wasExpected = true;
}
var i = -1, definition;
while ((definition = definitions[++i])) {
if (definition.match(method, url, data, headers || {})) {
if (definition.response) {
// if $browser specified, we do auto flush all requests
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
} else if (definition.passThrough) {
originalHttpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers);
} else throw new Error('No response defined !');
return;
}
}
throw wasExpected ?
new Error('No response defined !') :
new Error('Unexpected request: ' + method + ' ' + url + '\n' +
(expectation ? 'Expected ' + expectation : 'No more request expected'));
}

How to get all details of exception including errorMsg, url & lineNumber using $exceptionHandler?

I want to setup JS exception logging module in my angularJS application and for this I am using $exceptionHandler.
I am using following code to log app errors :
app.config(function($provide) {
$provide.decorator("$exceptionHandler", function($delegate) {
return function(exception, cause) {
$delegate(exception, cause);
// alert(exception.message);
console.log(JSON.stringify(exception.message += ' (caused by "' + cause + '")'));
};
});
});
But here,I am getting only message but I want all details related to exception like errorMsg, url, line number etc.
How to get all this details using above code?
As it turns out the only part of the error object that's standardized is the message. Both lineNumber, and fileName will give inconsistent results across different browser versions.
The most general way of getting as much detail about the exception that you can might be this:
app.config(function($provide) {
$provide.decorator("$exceptionHandler", function($delegate) {
return function(exception, cause) {
$delegate(exception, cause);
var formatted = '';
var properties = '';
formatted += 'Exception: "' + exception.toString() + '"\n';
formatted += 'Caused by: ' + cause + '\n';
properties += (exception.message) ? 'Message: ' + exception.message + '\n' : ''
properties += (exception.fileName) ? 'File Name: ' + exception.fileName + '\n' : ''
properties += (exception.lineNumber) ? 'Line Number: ' + exception.lineNumber + '\n' : ''
properties += (exception.stack) ? 'Stack Trace: ' + exception.stack + '\n' : ''
if (properties) {
formatted += properties;
}
console.log(formatted);
};
});
});

Why Array.length does not work with key string

I've been reading and doing some testing and found that the command. "Length" of the Array () javascript does not work when the array's keys are string. I found that to run this command, I use whole keys.
However, I wonder if any of you can tell me why this rule? Limitation of language, or specification of logic? Or, my mistake ...?
Thanks
Obs.: The key to my array is a string (name of component) but the values and the array of objects.
Declarating:
objElementos = new Array();
Object:
objElemento = function(Id){
this.Id = $('#' + Id).attr('id');
this.Name = $('#' + Id).attr('name');
this.CssLeft = $('#' + Id).css('left');
this.CssBottom = $('#' + Id).css('bottom');
this.Parent = $('#' + Id).parent().get(0);
this.Childs = new Array();
this.addChild = function(IdObjChild) {
try {
if ($('#' + IdObjChild).attr('id') != '')
this.Childs[$('#' + IdObjChild).attr('id')] = new objElemento(IdObjChild);
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.getChildCount = function(){
try {
$('#divErrors').prepend('<p>' + this.Childs.length + '</p>');
return this.Childs.length;
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.updateCssLeft = function(CssPosition){
try {
$('#divErrors').prepend('<p>updateCssLeft:' + this.Id + ' / ' + this.CssLeft + '</p>');
$('#' + this.Id).css('left',CssPosition);
this.CssLeft = $('#' + this.Id).css('left');
$('#divErrors').prepend('<p>updateCssLeft:' + this.Id + ' / ' + this.CssLeft + '</p>');
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.updateCssBottom = function(CssPosition){
try {
$('#divErrors').prepend('<p>updateCssBottom:' + this.Id + ' / ' + this.CssBottom + '</p>');
$('#' + this.Id).css('bottom',CssPosition);
this.CssBottom = $('#' + this.Id).css('bottom');
$('#divErrors').prepend('<p>updateCssBottom:' + this.Id + ' / ' + this.CssBottom + '</p>');
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
}
Use:
objElementos['snaptarget'] = new objElemento('snaptarget');
When using string "indexers" on an array, you are effectively treating it as an object and not an array, and tacking extra fields onto that object. If you are not using integer indexes, then there's not really much point in using the Array type and it makes more sense to use the Object type instead. You could count the fields as follows:
var c=0;
for(var fieldName in obj)
{
c++;
}
Since arrays with key strings are objects, you can use:
Object.keys(objElementos).length
The reason is because when you are using strings as your keys, you're really declaring an object, rather than an array. As such, there is no length attribute for objects.

Resources