Note that there are some explanatory texts on larger screens.

plurals
  1. POObject variables are not changing in the innerscope
    text
    copied!<p>I've made a function called Product which I use to create objects in JavaScript. Now I have a function called setProduct which gets data from my database (websql) and sets all these data from the database in the current object. This is my code:</p> <pre><code>function Product(nr, name, description, brand, category, price, tags, color, seccolor, image, sizes, db) { this.nr = nr; this.name = name; this.description = description; this.brand = brand; this.category = category; this.price = price; this.tags = tags; this.color = color; this.seccolor = color; this.image = image; this.sizes = sizes; this.db = db; Product.prototype.setProduct = function(id, cb) { var self = this; var query = "SELECT * from products WHERE id='"+id+"';" var res; this.db.exec(query, function(results) { res = results.rows.item(0); self.nr = res.prod_nr; self.description = res.prod_description; self.brand = res.prod_brand; self.category = res.prod_category; self.price = res.prod_price; self.tags = res.prod_tags; self.color = res.prod_color; self.seccolor = res.prod_sec_color; self.image = res.prod_image; self.sizes = res.available_sizes; self.name = res.prod_name; cb(true); }); }; } </code></pre> <p>this.db = my database object. setProduct calls the exec function from that object. That's this code:</p> <pre><code>Database.prototype.exec = function(query, cb) { this.db.transaction(function(tx) { tx.executeSql(query, [], function(tx, results) { if (typeof(cb) == 'function') { cb(results); } }); }); } </code></pre> <p>I've tried to use several console.logs to see where the data gets lost, but I can't find it somehow. </p> <p>And here is where it goes wrong:</p> <pre><code>$("div#home").off('click').on('click','li', function() { var product = prod; var prod_id = $(this).attr("id"); prod.setProduct(prod_id, function(a) { if (a) { console.log(prod.name); // console.log gives undefined } }); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload