null is not an object - jQuery + leaflet - javascript

I'm trying to get some data using a query build like this:
function buildQuery(startDate, endDate)
{
/*http://data.cityofnewyork.us/resource/erm2-nwe9.json?$where=(latitude%20IS%20NOT%20NULL)%
20AND%20(complaint_type%20like%20%27\%Noise\%%27)%20AND%20(created_date%3E=%272013-08-01%27)%
20AND%20(created_date%3C=%272013-08-08%27)&$group=complaint_type,descriptor,latitude,longitude&$
select=descriptor,latitude,longitude,complaint_type*/
var start_date = formattedDate(startDate); //YYYY-MM-DD
var end_date = formattedDate(endDate); //YYYY-MM-DD
var c_type = 'Noise'; // Complaint Type
// Build the data URL
URL = "http://data.cityofnewyork.us/resource/erm2-nwe9.json"; // API Access Endpoint
URL += "?"; // A query parameter name is preceded by the question mark
URL += "$where="; // Filters to be applied
URL += "(latitude IS NOT NULL)"; // Only return records with coordinates
URL += " AND ";
URL += "(complaint_type like '\\%" + c_type + "\\%')";
URL += " AND ";
URL += "(created_date>='" + start_date + "') AND (created_date<='" + end_date + "')"; // Date range
URL += "&$group=complaint_type,descriptor,latitude,longitude"; // Fields to group by
URL += "&$select=descriptor,latitude,longitude,complaint_type"; // Fields to return
URL = encodeURI(URL); // Encode special characters such as spaces and quotes
URL = URL.replace("'%5C%25", "%27\\%"); // Only way that seems to work in Safari
URL = URL.replace("%5C%25'", "\\%%27");
}
Then, I plot the latitude / longitude of the response as follows:
function load311ComplaintsIntoMap(map)
{
cleanMap();
$.getJSON(URL, function(data)
{
if ( data.length == 0 )
{
return;
}
var markers = []
for (var i = 0; i < noise_description.length; i++)
{
markers[i] = [];
}
var all_markers = [];
$.each(data, function(index, rec)
{
var marker;
for (var i = 0; i < noise_description.length; i++)
{
if (rec.descriptor.indexOf(noise_description[i]) > -1)
{
marker = L.circleMarker([rec.latitude, rec.longitude], marker_style(i));
markers[i].push(marker);
all_markers.push(marker);
break;
}
if (i == noise_description.length-1)
{
marker = L.circleMarker([rec.latitude, rec.longitude], marker_style(i));
markers[i].push(marker);
all_markers.push(marker);
}
}
});
// Create layer of all markers but do not add to map
var all_layers = L.featureGroup(all_markers);
// Create specific layers of markers and add to map
for (var i = 0; i < markers.length; i++)
{
layers[i] = L.featureGroup(markers[i]).addTo(map);
layers[i].bringToFront();
} // 311complaints.js:152
map.fitBounds(all_layers.getBounds());
for (var i = 0; i < noise_description.length; i++)
{
overlays['<i style="background:' + getColor(i) + '"></i> ' +noise_description[i]] = layers[i];
}
// Add layer control using above object
layer = L.control.layers(null,overlays).addTo(map);
});
}
However, I'm receiving this error:
TypeError: null is not an object (evaluating 't.lat')
projectleaflet.js:5:17200
latLngToPointleaflet.js:5:17604
projectleaflet.js:5:24291
latLngToLayerPointleaflet.js:5:24556
projectLatlngsleaflet.js:7:15193
redrawleaflet.js:6:28501
setRadiusleaflet.js:7:15524
_updateStyleleaflet.js:7:15290
_initStyleleaflet.js:6:30051
_initElementsleaflet.js:6:29400
onAddleaflet.js:6:27822
_layerAddleaflet.js:5:29679
addLayerleaflet.js:5:21183
eachLayerleaflet.js:6:25647
onAddleaflet.js:6:25458
_layerAddleaflet.js:5:29679
addLayerleaflet.js:5:21183
addToleaflet.js:6:25578
(anonymous function)311complaints.js:152
jjquery-2.1.0.min.js:1:26681
fireWithjquery-2.1.0.min.js:1:27490
xjquery-2.1.0.min.js:3:10523
(anonymous function)jquery-2.1.0.min.js:3:14160
And I don't know why, since I request fields that have a valid latitude.

Some of the objects that you get back from the API does not have a latitude.
Check that they do before adding it to the map:
rec.hasOwnProperty("latitude") && rec.hasOwnProperty("longitude")

Related

How do I split long message, and send them in sequence?

Disclaimer I am a newbie at programing and also not really good at english, so please pardon my bad explanation.
My bot had to send a message containing data retrieved from a spreadsheet, but found that the data was so many that it exceeded the character limit of the telegram message (4096 characters).
So I assume that I have to split the message, then send them in sequence. Please help...
here's my code :
function SearchbyProjectName(Prjct){
var projectNON = GetAllProjectNON();
var project = GetAllProject();
var num = 1;
const allLocation = [];
var header =
'xxxheaderxxx';
var footer =
'xxxfooterxxx'
for (var row=0; row<projectNON.length; row++) {
if(projectNON[row][3]==Prjct){
var resiNON = num++ + '. ) ' + '/d_' + projectNON[row][6] + '\n\n';
allLocation.push(resiNON);
}
}
for (var row=0; row<project.length; row++) {
if(project[row][3]==Prjct){
var resi = num++ + '. ) ' + '/d_' + project[row][6] + '\n\n';
allLocation.push(resi);
}
}
var stringg = allLocation.toString();
var clearComma = stringg.replaceAll(",", "")
var newData = header + clearComma + footer;
return newData ;
}
here's the message :
ALL LOCATION
Project: PTT2
🗓 : Tuesday, 24 Jan 2023 15:10:34
) /d_XXXXXXXXXXX
) /d_XXXXXXXXXXXXX
) /d_XXXX
) /d_DDDDDD
) /d_JJJJJJJJJJJJJ
) /d_XXXXXXX
...
) /d_BBBBBBBBBBBBB
I wish I could split the message at number 100.)
I'm not able to test this but I think it might solve your problem. For every 100 projects store the string in the array newData. Then in the receiver of newData loop throught the array of strings and send each individually.
Notice I don't push strings to the array allLocation but simply concatinate strings. Once I've reach a multiple of 100 push that concatinated string to the array newData.
function SearchbyProjectName(Prjct){
var projectNON = GetAllProjectNON();
var project = GetAllProject();
var num = 1;
var newData = [];
var allLocation = "xxxheaderxxx";
var footer = "xxxfooterxxx";
var limit = 100;
for (var row=0; row<projectNON.length; row++) {
if(projectNON[row][3]==Prjct){
var resiNON = num++ + '. ) ' + '/d_' + projectNON[row][6] + '\n\n';
allLocation.concat(resiNON);
if( num === limit ) {
newData.push(allLocation);
limit = limit+100;
allLocation = "";
}
}
}
for (var row=0; row<project.length; row++) {
if(project[row][3]==Prjct){
var resi = num++ + '. ) ' + '/d_' + project[row][6] + '\n\n';
allLocation.concat(resi);
if( num === limit ) {
newData.push(allLocation);
limit = limit+100;
allLocation = "";
}
}
}
allLocation = allLocation.concat(footer);
newData.push(allLocation);
return newData;
}
Reference
String.concat()

How can I add a hyperlink to a javascript message?

Im trying to send an email from google sheets and I've setup the columns to represent the subject, text and email addresses. The problem is that I need to add a hyperlink in the middle of the message and im stuck here. I can't get the paragraph to format correctly AND have the hyperlink replace a word in the middle of the sentence.
This is the code:
function AISEMAIL() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Email Addresses');
var sheet2=ss.getSheetByName('Email Fields');
var subject = sheet2.getRange(2,1).getValue();
var message = sheet2.getRange(2,2).getValue();
var calendardisplayname = sheet2.getRange(2,3).getValue();
var calendarlink = sheet2.getRange(2,4).getValue();
var formdisplayname = sheet2.getRange(2,5).getValue();
var formlink = sheet2.getRange(2,6).getValue();
message=message.replace("<calendar>",calendardisplayname).replace("<form>",formdisplayname);
var n=2;
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
let options = {
htmlBody: message
+ '' + calendardisplayname + ''
+ '' + formdisplayname + ''
}
GmailApp.sendEmail(emailAddress, subject, message,options);
}
}
Problem:
You are always trying to append the links at the end on your options. Also, it becomes redundant. What you need to do is include the links when you replace the values of <calendar> and <form>.
Code:
// Add the links on the replace
message = message
.replace("<calendar>", '' + calendardisplayname + '')
.replace("<form>", '' + formdisplayname + '');
var n = 2;
for (var i = 2; i < n + 1; i++) {
var emailAddress = sheet1.getRange(i, 1).getValue();
let options = {
htmlBody: message
}
GmailApp.sendEmail(emailAddress, subject, message, options);
}
Old Output:
New Output:
This is just a guess, but could you use HtmlService to create your html message?
function AISEMAIL() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Email Addresses');
var sheet2=ss.getSheetByName('Email Fields');
var subject = sheet2.getRange(2,1).getValue();
var message = sheet2.getRange(2,2).getValue();
var calendardisplayname = sheet2.getRange(2,3).getValue();
var calendarlink = sheet2.getRange(2,4).getValue();
var formdisplayname = sheet2.getRange(2,5).getValue();
var formlink = sheet2.getRange(2,6).getValue();
message=message.replace("<calendar>",calendardisplayname).replace("<form>",formdisplayname);
var n=2;
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
//ADDED htmlText VARIABLE
var htmlText;
let options = {
// ADDED HtmlService.createHtmlOutput()
htmlBody: htmlText = HtmlService.createHtmlOutput(message
+ '' + calendardisplayname + ''
+ '' + formdisplayname + '');
}
// CHANGED message TO htmlText
GmailApp.sendEmail(emailAddress, subject, htmlText,options);
}
}
REFERENCES
HtmlService
Also, if you need to create dynamic html for each email, check out these links...
createTemplateFromFile()
.evaluate()
Templated HTML

How do I access variable while looping through

I have this piece of code
(function() {
'use strict';
// Returns current URL page.
var currentURL = window.location.protocol + "//" + window.location.host + "/" +
window.location.pathname + window.location.search
var market_table = document.getElementById("market_item_table");
var market_table_cells = document.getElementsByTagName("td");
var rowLength = market_table.rows.length;
var items = document.querySelectorAll('div[data-item-id]')
var j = 0;
items.forEach(function(getItem) {
var itemData = items[j].dataset;
var tooltip = JSON.parse(itemData.tooltip)[0][0][0];
console.log(tooltip);
j++
});
for (var i = 1; i < rowLength; i++) {
var oCells = market_table.rows.item(i).cells;
console.log("Paka nr. " + i + "Nazwa przedmiotu: " + tooltip + ' ' +
oCells[1].innerText + ' ' + oCells[2].innerText);
}
})();
Now how would I go about and get the value of variable tooltip every time it loops through the NodeList?
The bigger picture is that I want to assign the correct names to the correct items listed.
You can set up an array outside of the for loop that stores the values of the tooltips, then use it in a separate function:
var tooltips = [];
Items.forEach(function(getItem) {
var itemData = getItem.dataset;
var tooltip = JSON.parse(itemData.tooltip)[0][0][0];
console.log(tooltip);
});
for (var i=0; i < rowlength... {
// Do something with the tooltips
console.log(tooltips[i]);
}

How to remove empty values from array in google docs script

I am trying to print out the values of the array in a google doc. I do get the correct values but it goes on printing out a number of "undefined" values. The simplest way is probably to filter out the undefined values before I print out the array.
Here is the array declaration:
var paramArr = Object.keys(e.parameter).reverse();
var tableArr = [];
for(var i = 0; i < paramArr.length - 1; i++) {
var tempArr;
var nameSelector = "Company:" + i;
var startDateSelector = "Started:" + i;
var endDateSelector = "Ended:" + i;
var referenceSelector = "Reference:" + i;
var descriptionSelector = "Description:" + i;
tempArr = [e.parameter[nameSelector] + " ",
e.parameter[startDateSelector] + " - " +
e.parameter[endDateSelector]+ "\n\n" +
e.parameter[descriptionSelector]
];
I have tried this, but it doesn't work:
tempArr = tempArr.filter(function(element){
return element !== undefined;
});

How can I pass time using jquery

I have 2 pages. On page 1 i have two values, one is string and one is datetime.
I want to pass that value to page 2 but i have problem, the string is passed but datetime is not passed. This is my code
time = jQuery('#timepicker_7').val();
var url = "test.aspx?name=" + encodeURIComponent(lObjSeat[0].Name) + "&technology=" + encodeURIComponent(time);
window.location.href = url;
this is what's on page 2
<script type="text/javascript">
var queryString = new Array();
$(function () {
if (queryString.length == 0) {
if (window.location.search.split('?').length > 1) {
var params = window.location.search.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var key = params[i].split('=')[0];
var value = decodeURIComponent(params[i].split('=')[1]);
queryString[key] = value;
}
}
}
if (queryString["name"] != null) {
var data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Name:</b> " + queryString["name"] + " <b>Technology:</b> " + queryString["technology"];
$("#lblData").html(data);
}
});
"name" is done. but "time" is not show. whats the problem ?

Categories

Resources