how to skip undefined properties in JSON? - json

When I parse JSON fields coming from google maps, etc., it is a mess. Because they are not made specifically for my script I have to verify many details, epecially because the addresses are different in every country.
Short question: when the script finds a undefined property the script breaks...error..
How can I verify the property is defined?
if(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality != null) {
/***do something***/
}
Something like that doesn't seem to solve the problem. Why?

In JavaScript, accessing a property of an object that does not exist returns undefined, not null - heck, you said it in the title.
So, assuming that all the previous properties do actually exist, you can check that the Locality property exists using typeof, like this:
if(typeof (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.
Locality) !== 'undefined') {
/***do something***/
}
Or, (I think) you can use hasOwnProperty():
if (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.hasOwnProperty('Locality'))
{
/*** do something ***/
}

First, In javascript you can use "try / catch" like java or other programming language, this can let your code continue running if something goes wrong...
for your issue, you can test that :
if (typeof(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality)
&&
data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality.length>0) {
}

Related

How do I find the name of this control? "Cannot read property 'focus' of null"

I am trying to focus an element of web.whatsapp.com.
I use "Inspect" in Chrome to find its name.
As I read it, the name is
div.Er7QU.copyable-text selectable-text"
However, when I call ".focus()" on it...
document.querySelector("div.Er7QU.copyable-text selectable-text").focus();
I receive the error
Cannot read property 'focus' of null.
What am I doing wrong?
In Chrome, it looks like this:
You must use document.querySelector("div.Er7QU.copyable-text.selectable-text").focus();
You need to concatenate all class names via the . (also, this is exactly what appears on your screenshot. There are no spaces between the classes)
Also, as #Rick mentioned, make sure to protect the code by using a conditional statement that will check whether the selector returned null before running any methods on the element selected.
document.querySelector("div.Er7QU.copyable-text selectable-text") is returning null. code it like this to avoid errors, but something else is wrong with your code:
var el = document.querySelector("div.Er7QU.copyable-text selectable-text");
if (el)
{
el.focus();
}
else
{
alert('Element not found.');
}

Angular: Routing between pages using condition

I am trying to route between pages using basic if condition in Angular.
GoToHome() {
if(this.router.url=='/chat'){
console.log(this.router.url)
this.router.navigate(['login']);
} else {
this.router.navigate(['people']);
}
}
The problem is that the route chat isn't really correct, there are many pages in chat (chat\x , chat\y and many others) I want that it will work for all the pages in chat, but right now it doesn't work. If I write a specific route like chat\x it does work, but only for x. Is there a way to do it for all?
you can read and check Guards. Read about CanActivate method, maybe it will help you?
RouteGuards might do a better job of handling the redirects as per your requirement.
But a quick workaround would be to do a split() on the URL and compare for the chat part. Try the following
if(((this.router.url).split('/')[1]) === 'chat') {
// proceed
}
As other had said, best solution is to use Angular Guard https://medium.com/#ryanchenkie_40935/angular-authentication-using-route-guards-bf7a4ca13ae3.
Anyway to resolve your problem you can use startsWith() function which determines whether a string begins with the characters of a specified string.
GoToHome() {
if((this.router.url).startsWith('/chat'){
console.log(this.router.url)
this.router.navigate(['login']);
} else {
this.router.navigate(['people']);
}
}

Linq to SQL - Turn off UpdateCheck in code

I am wanting to turn off the UpdateCheck functionality for all members (except their primary keys). Now I was following the example below as guidance, however my MetaDataMembers of the table are still set to Always.
http://www.the-lazy-coder.com/2013/04/set-updatecheck-to-never.html
The above code snippet just gets you to change the attribute, however it seems to never get picked up, as I can debug the code when it is running and I see all the properties being set, so I am presuming that the attributes changing does not change the underlying object.
Now if I were to change approach and just get the MetaDataMembers directly from the RowType I notice they have the UpdateCheck property, however only a getter. So is there a way to (via reflection if needed) overwrite this property once it is set? Even after looking at decompiled source it is an abstract class and I cannot find any implementations to use for reference.
I am using SQLMetal to generate the Context files, so there is no designer tinkering available, and although some people will say that I should run some text editing macros to parse and change the attributes, it all sounds too long winded when I should just be able to go into the object in memory and tell it to ignore whatever it has been told previously.
SO! Is there a way to override the property in the entities? I have tried running the original code in that link in both constructor, after the objects created and just before I am about to do an update, however none of the changes seem to stick or at least propagate to where it matters, and there is hardly any material on how to do any of this progmatically.
After searching around the internet I found no nice way to do it, and although there is the link I mentioned originally it doesn't work as it works on the attributes which are partly right but in the case above they are working on the attributes which are not in memory and are just the decorations, anyway the code below seems to work but is not nice:
public static void SetUpdateCheckStatus(this IDataContext dataContext, UpdateCheck updateCheckStatus)
{
var tables = dataContext.Mapping.GetTables();
foreach (var table in tables)
{
var dataMembers = table.RowType.DataMembers;
foreach (var dataMember in dataMembers)
{
if (!dataMember.IsPrimaryKey)
{
var dataMemberType = dataMember.GetType();
if (dataMemberType.Name == "AttributedMetaDataMember")
{
var underlyingAttributeField = dataMember.GetType().GetField("attrColumn", BindingFlags.Instance | BindingFlags.NonPublic);
if (underlyingAttributeField != null)
{
var underlyingAttribute = underlyingAttributeField.GetValue(dataMember) as ColumnAttribute;
if (underlyingAttribute != null)
{ underlyingAttribute.UpdateCheck = updateCheckStatus; }
}
}
else
{
var underlyingField = dataMember.Type.GetField("updateCheck", BindingFlags.Instance | BindingFlags.NonPublic);
if (underlyingField != null)
{ underlyingField.SetValue(dataMember, updateCheckStatus); }
}
}
}
}
}
The IDataContext is just a wrapper we put around a DataContext for mocking purposes, so feel free to change that to just DataContext. It is written extremely defensively as this way pulls back lots of members which do not have all the desired data so it has to filter them out and only work on the ones which do.

Filtering the iNotes Calendar in extlib

I need too filter the iNotes calendar control in extlib. When I look in the examples in the extlib application I can see that it is suppose to be connected to a xecalendarJsonLegacyService.
The problem I find with this service is that I can't filter the content based on category or search as with the other view services.
I need to create different calendars/json data based on a search or category in a view.
I have looked at some of the other services but not sure if it is possible to use them instead.
If you have any ideas for how I should create my filter, please respond.
I have attached pictures below showing both the jsonservice and the calendarcontrol.
This is what the json data look like in the xsCalendarJsonLegacyService
{
"#timestamp":"20120311T171603",
"#toplevelentries":"3",
"viewentry":
[
{
"#unid":"37F0330979C04AF2C12579BE004F5629",
"#noteid":"32E1A",
"#position":"1",
"#read":"true",
"#siblings":"3",
"entrydata":
[
{
"#columnnumber":"0",
"#name":"$134",
"datetime":
{
"0":"20120314T100000"
}
},
{
"#columnnumber":"1",
"#name":"$149",
"number":
{
"0":119
}
}, etc...
You could implement your own REST service (or extension to existing one) in an extension library, but I guess you are looking for something easier.
Sorry no code, but maybe (and hopefully) an answer.
Have you looked at the xc:CalendarStoreCustomRestService custom control inside the Xpages Extension Library demo? It looks like they connected the calendar control with a normal JSON view store and that supports search en keys.
I found code you could use but you will have to extend the custom control. I think it is a new component that is not yet included as a xe: component inside the Extension Library.
This is how you use the control:
<xc:CalendarStoreCustomRestService id="cc4ccCalendarStoreCustomRestService"
storeComponentId="notesCalendarStore1" databaseName="#{sessionScope.databaseName}"
viewName="($Calendar)">
</xc:CalendarStoreCustomRestService>
This is your calendar component, it uses the above storeComponentId.
<xe:calendarView id="calendarView1" jsId="cview1"
summarize="false"
type="#{javascript: null == viewScope.calendarType? 'M' : viewScope.calendarType }"
storeComponentId="notesCalendarStore1">
<xe:this.loaded><![CDATA[${javascript:if (sessionScope.databaseName == null) {
return false;
} else {
return true;
}}]]></xe:this.loaded>
</xe:calendarView>
If you need some more info, this example is included inside the DWA_iNotesRest.xsp.
I googled a long time and the only solution I`ve found is to build your own Rest service
have you managed to filter the Calendar without this?

Stomp - multiple subscriptions each with a unique handler

I am using Stomp / Orbited for Comet functionality.
In order to deal with multiple channels, I end up doing this:
stomp.onmessageframe = function(frame) {
if (frame.headers['destination'] == '/thisFeed/') {
//handle thisFeed
}
if (frame.headers['destination'] == '/thatFeed/') {
//handle thatFeed
}
....which is OK, I guess. But what if I don't know, at load time, how I want to handle a feed? I want to be able to do something like this:
stomp.subscribe('someOtherFeed', someOtherFeedHandler);
That way, when I subscribe, I can define the handler then and only then.
I have come up with one solution, but it's so very far from pretty.
When I create the stomp message, I add a "handler" property as a header, like so in python:
conn.send('Frank the Wonder Llama", destination="/infoAboutLlamas/", handler='llamas')
Then, in the javascript:
stomp.onmessageframe = function(frame) {
window[frame.headers['handler']]() //Execute the function named by the handler
}
...so then, the function llamas() gets called. I can then define (and redefine) llamas anywhere I want.
Now I'm sure this can't be the optimal solution. I do, on the other hand, like that it gives me a bit of flexibility to specify the handler I want to use right in python. But seriously, I'm thinking there is a better way.