Here’s a link to the github for my project, in case the snippet I’ll post in a sec doesn’t give enough info:
Stamp Inventory Github Link
Ok, so I’m getting data from a Google spreadsheet and displaying it in my html page. Works great. As I go through each name/value pair, I add it to an array called stamps. I’m console logging the array at the end and it contains all the values. Seems good.
So then, when I submit one of my forms to update the values, I’m getting the values from the form just fine. All console logging seems to look right. But I go to find the index of each name in stamps…and it keeps returning -1. So somehow, it’s not seeing it! No idea why. The spelling is the same, capitalization is the same, I even tried trimming both values before adding/searching in case there was extra white space on the ends…nothing. It doesn’t see it.
I’m at my wits end. I’m obviously missing something, but…I don’t know what. This problem shows up both on my localhost and my cloud9 workspace, using the same code. I’m using Chrome both times. I mean, maybe it’s Chrome for some odd reason? Though I just tried it in Firefox, and it’s having the same problem. I don’t know. Someone please help if you can. Here’s a snippet that involves the code in question (the stamps array is a global declared at the very beginning, not shown below, just a blank array):
function getData() {
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: '1qnHMLUQRRWyQMMLWDyuKfRHcGVWMJzhNkorTnmzSbjk',
range: 'forSite!A2:B14',
}).then(function (response) {
var range = response.result;
if (range.values.length > 0) {
for (var i = 0; i < range.values.length; i++) {
var name = range.values[i][0];
var qty = range.values[i][1];
document.getElementById(name).innerHTML = qty;
stamps.push( [name , qty ] );
}
}
console.log("Starting array: " + stamps);
}, function (response) {
console.log('Error: ' + response.result.error.message);
});
}
$("#addUsed").submit(function (event) {
event.preventDefault();
var values = $(this).serializeArray();
console.log(values);
for (var a = 0; a <= counter; a++) {
var name = values[a].value;
var qtyused = values[a + 1].value;
var spot = stamps.indexOf(name);
console.log(name, qtyused, spot);
}
});