I can't seem to get data from my taffyDB.
It has to be bad syntax but I don't know where.
My Javascript looks like this:
// init db
var procTech = TAFFY();
//..... other code in the middle
procTech().remove();
var x = 0;
$(".sxRow select[id^='KRCtech_']").each(function() {
var techName = $(this).val();
x++;
var xStr = x.toString();
clog(xStr + " " + techName);
procTech.insert({"count":xStr,"tech":techName });
});
var ret = eval(procTech().select("count","tech"));
clog(ret.length);
for (j = 0; j <= ret.length - 1; j++) {
clog("read back: " + [j][0] + "," + [j][1]);
}
// wrapper for console.log
function clog(s) {
window.console && console.log(s);
return;
}
console says:
1 tonya
2 shawn
2
read back: 0,undefined
read back: 1,undefined
so I know that
my initial values are good; i.e. they have value
Taffy sees 2 records, which is correct
It's just when I try and retrieve them, they are garbage.
What am I doing wrong?
procTech.insert({"count":1,"tech":'techName' });
procTech.insert({"count":2,"tech":'techName1' });
procTech.insert({"count":3,"tech":'techName2' });
procTech.insert({"count":3,"tech":'techName2' });
procTech.insert({"count":3,"tech":'techName2' });
procTech.insert({"count":4,"tech":'techName3' });
var query = procTech.select("count","tech"); // 3 rows
for ( var x=0; x<query.length-1; x++ ) {
console.log(query[x][0], query[x][1]);
}
Related
I don't understand how to solve the following problem when return the value from nested function. is it wrong method. How can I get it?
My basic purpose is to get array values (all coordinates) from var mymap_coordinates, but it can't. that's why I use .toString() to test.
*<script>
mymap.on( //leftlet code
'contextmenu',
function (event)
{
var tg_marker = L.marker(event.latlng, {icon: treegroupIcon}).addTo(mymap);
store_coordinates[incre_coord] = new Point(tg_marker.getLatLng().lat.toFixed(8), tg_marker.getLatLng().lng.toFixed(8));
var n = store_coordinates.length;
var mymap_coordinates = abcdefg(store_coordinates, n);
window.alert (mymap_coordinates.toString()); //This alert can't print all array value when return the value from abcdefg function
incre_coord = incre_coord + 1;
}
);
function abcdefg(points, n)
{
.......
.......
.......
// return Result
var num = 0;
var map_coordinate = new Array();
for (let temp of abc.values())
{
map_coordinate[num] = "[" + temp.x + ", " + temp.y + "]";
num = num + 1;
}
window.alert (map_coordinate.toString()); //This alert can print all array value
return map_coordinate;
}
</script>*
Please help....Tried executing the below mentioned function but web console always shows
TypeError: xml.location.forecast[j] is undefined
I was able to print the values in alert but the code is not giving output to the browser because of this error. Tried initializing j in different locations and used different increment methods.How can i get pass this TypeError
Meteogram.prototype.parseYrData = function () {
var meteogram = this,xml = this.xml,pointStart;
if (!xml) {
return this.error();
}
var j;
$.each(xml.location.forecast, function (i,forecast) {
j= Number(i)+1;
var oldto = xml.location.forecast[j]["#attributes"].iso8601;
var mettemp=parseInt(xml.location.forecast[i]["#attributes"].temperature, 10);
var from = xml.location.forecast[i]["#attributes"].iso8601;
var to = xml.location.forecast[j]["#attributes"].iso8601;
from = from.replace(/-/g, '/').replace('T', ' ');
from = Date.parse(from);
to = to.replace(/-/g, '/').replace('T', ' ');
to = Date.parse(to);
if (to > pointStart + 4 * 24 * 36e5) {
return;
}
if (i === 0) {
meteogram.resolution = to - from;
}
meteogram.temperatures.push({
x: from,
y: mettemp,
to: to,
index: i
});
if (i === 0) {
pointStart = (from + to) / 2;
}
});
this.smoothLine(this.temperatures);
this.createChart();
};
You are trying to access the element after the last one. You can check if there is the element pointed by j before proceeding:
Meteogram.prototype.parseYrData = function () {
var meteogram = this,
xml = this.xml,
pointStart;
if (!xml) {
return this.error();
}
var i = 0;
var j;
$.each(xml.location.forecast, function (i, forecast) {
j = Number(i) + 1;
if (!xml.location.forecast[j]) return;
var from = xml.location.forecast[i]["#attributes"].iso8601;
var to = xml.location.forecast[j]["#attributes"].iso8601;
});
};
Simply my code looks like this:
var thevariable = 0;
For(){
//somecode using thevariable
$.getJSON('',{},function(e){
//success and i want to set the returned value from php to my variable to use it in the forloop
thevariable = e.result;
});
}
my main problem that the variable value stays "0", during the whole For loop, while i only want it to be "0" at the first loop, then it takes the result returned from PHP to use it on for loop.
here it my real code if you need to take a look:
var orderinvoice = 0;
for(var i=0; i<table.rows.length; i++){
var ordername = table.rows[i].cells[5].innerText;
var orderqty = ((table.rows[i].cells[1].innerText).replace(/\,/g,'')).replace(/Qty /g,'');
var orderprice = (table.rows[i].cells[2].innerText).replace(/\$/g,'');
var ordertype = table.rows[i].cells[3].innerText;
var orderlink = table.rows[i].cells[4].innerText;
$.getJSON('orderprocess.php', {'invoice': orderinvoice, 'pay_email': email, 'ord_name': ordername, 'ord_qty': orderqty, 'ord_price': orderprice, 'ord_type': ordertype, 'ord_link': orderlink}, function(e) {
console.log();
document.getElementById("result").innerText= document.getElementById("result").innerText + "Order #"+e.result+" Created Successfully ";
document.getElementById("invoker").innerText = ""+e.invoice;
orderinvoice = e.invoice;
if(i+1 == table.rows.length){
document.getElementById("result").innerText= document.getElementById("result").innerText + "With invoice #" + e.invoice;
}
});
in a loop block, before one ajax complete other one will be run and this's javascript natural treatment. For your case you can call a function at the end of success event. Do something like this:
var i = 0;
doSt();
function doSt() {
var orderinvoice = 0;
var ordername = table.rows[i].cells[5].innerText;
var orderqty = ((table.rows[i].cells[1].innerText).replace(/\,/g, '')).replace(/Qty /g, '');
var orderprice = (table.rows[i].cells[2].innerText).replace(/\$/g, '');
var ordertype = table.rows[i].cells[3].innerText;
var orderlink = table.rows[i].cells[4].innerText;
$.getJSON('orderprocess.php', { 'invoice': orderinvoice, 'pay_email': email, 'ord_name': ordername, 'ord_qty': orderqty, 'ord_price': orderprice, 'ord_type': ordertype, 'ord_link': orderlink }, function(e) {
console.log();
document.getElementById("result").innerText = document.getElementById("result").innerText + "Order #" + e.result + " Created Successfully ";
document.getElementById("invoker").innerText = "" + e.invoice;
orderinvoice = e.invoice;
if (i + 1 == table.rows.length) {
document.getElementById("result").innerText = document.getElementById("result").innerText + "With invoice #" + e.invoice;
}
i++;
if (i < table.rows.length) doSt();
});
}
I think you need a recursive function that always deals with the first element in your rows array and then splices it off and calls itself. For example, something like this:
function getStuff(rows, results) {
if (rows.length > 0) {
var ordername = rows[0].cells[5].innerText;
$.getJSON('orderprocess.php', { 'ord_name': ordername }, function (e) {
// do some stuff
results.push('aggregate some things here?');
rows.splice(0, 1);
return getStuff(rows, results);
});
} else {
return results;
}
}
When the array is spent, results will be returned with whatever aggregate you wanted at the end of the cycle. Then, you can do as you please with the results. I think you can also manipulate the DOM inside the function as you see fit if that makes more sense. Hope this helps.
I have a little piece of code that reads some ajax (this bit works) from a server.
var self = this;
var serverItems = new Array();
var playersOnlineElement = $("#playersOnline");
function DataPair(k, v) {
this.key = k;
console.log("new datapair: " + k + ", " + v);
this.value = v;
}
DataPair.prototype.getKey = function() {
return this.key;
}
DataPair.prototype.getValue = function() {
return this.value;
}
$.getJSON("http://127.0.0.1", function(data) {
$.each(data, function(key, val) {
var pair = new DataPair(key, val);
self.serverItems.push(pair);
});
});
console.log(serverItems.length); //Problem is here
for (var i = 0; i < serverItems.length; i = i + 1) {
var dpair = serverItems[i];
if (dpair.getKey() === "playersOnline") {
self.playersOnlineElement.text("Players Online: " + dpair.getValue());
}
}
The datapair and the JSON get loaded but when they are pushed to the array it doesn't seem to work. I tried with self.serverItems and just serverItems because netbeans showed me the scope of the variables being good if I used just serverItems but I am a bit confused as to why this doesn't work. Can anyone help me?
I put in comments where the error is. serverItems.length is 0 even though when debugging in a browser in the DOM tree it has an array serverItems with all the data inside.
Assumingly this serverItems is in another scope and not the one I am calling when I want to get the length?
add this code into the success part, since its asynchronous...
for (var i = 0; i < serverItems.length; i = i + 1) {
var dpair = serverItems[i];
if (dpair.getKey() === "playersOnline") {
self.playersOnlineElement.text("Players Online: " + dpair.getValue());
}
to...
$.getJSON("http://127.0.0.1", function(data) {
$.each(data, function(key, val) {
var pair = new DataPair(key, val);
self.serverItems.push(pair);
for (var i = 0; i < serverItems.length; i = i + 1) {
var dpair = serverItems[i];
if (dpair.getKey() === "playersOnline") {
self.playersOnlineElement.text("Players Online: " + dpair.getValue());
}
});
});
What I'm trying to do is to enable multiple checkboxes within for(). Right now it looks like this, but from what I have learned, you can't run the command from a variable like this, I can't run, (e.g) cab_type_value = "whatever". Nor can I run road_load_enabled; it just doesn't work. Does anyone how I can achieve this? How can I parse my var J in the document.MyForm.InputName.disabled?
for( var j=1; j<=14; j++ ) {
var cab_type_value = "document.exe_mode_form.cab_type" + j + ".value";
var cab_type_checked = "document.exe_mode_form.cab_type" + j + ".checked == 1";
for( var i=1; i<=document.exe_mode_form.road_load_number.value; i++ ) {
var road_load_value = "document.exe_mode_form.load" + i + ".value";
var road_load_enabled = "document.exe_mode_form.load" + i + ".disabled = false";
var road_load_disabled = "document.exe_mode_form.load" + i + ".disabled = true";
var rld_db = "document.exe_mode_form.a" + i + "_a1.value";
if ( cab_type_checked ) {
if ( test == 1 ) {
if(road_load_disabled) {
alert("road_load_disabled");
road_load_enabled;
}
test = 2;
}
if(cab_type_value == rld_db) {
if(olof == 1) {
alert("cab_type_value == rld_db");
olof = 2;
}
road_load_enabled;
}
}
}
}
Also, this part isn't working:
if(cab_type_value == rld_db) {
if(olof == 1){
alert("cab_type_value == rld_db");
olof = 2;
}
road_load_enabled;
}
And I have checked, cab_type_value has the same value as rld_db.
try changing these kind of lines:
var cab_type_value = "document.exe_mode_form.cab_type" + j + ".value";
to:
var cab_type_value = document.exe_mode_form['cab_type' + j].value;
The same logic holds for function calls:
someVariable = 'alert';
window[someVariable]('Foo!');//alerts foo
If your function is not declared in the global scope, you can replace window with any namespace object: foobarObject.orEven.nestedOnes[someVariable]();And finally, because I personally loathe the window keyword (it's a circular reference), you can simply choose to use this['alert']('foo'); in a regular function call or in the global scope. this points to its called context, which in these cases is the global object