alexiusacademia / electron-db

Electron module that acts as database management and uses flat file database (json file) to store tables.
MIT License
87 stars 24 forks source link

Cannot update row #13

Closed miladkian closed 5 years ago

miladkian commented 5 years ago

Update method only works on first record !!!

miladkian commented 5 years ago

I fixed it locally by move this two lines outside of rows loop callback(false, "Cannot find the specified record."); return; two lines comment here: ` for (var i = 0; i < rows.length; i++) { let matched = 0; // Number of matched complete where clause for (var j = 0; j < whereKeys.length; j++) { // Test if there is a matched key with where clause and single row of table if (rows[i].hasOwnProperty(whereKeys[j])) { if (rows[i][whereKeys[j]] === where[whereKeys]) { matched++; } } } if (matched === whereKeys.length) { // All field from where clause are present in this particular // row of the database table try { for (var k = 0; k < setKeys.length; k++) { rows[i][setKeys[k]] = set[setKeys[k]]; }

      // Create a new object and pass the rows
      let obj = new Object();
      obj[tableName] = rows;

      // Write the object to json file
      try {
        jsonfile.writeFile(fname, obj, {spaces: 2}, function (err){
          console.log(err);
        });
        callback(true, "Success!")
        return;
      } catch (e) {
        callback(false, e.toString());
        return;
      }

      callback(true, rows);
    } catch (e) {
      callback(false, e.toString());
      return;
    }
  } else {
   // callback(false, "Cannot find the specified record.");
   // return;
  }

}`
alexiusacademia commented 5 years ago

Hi there. Thanks for opening the issue. Can you please send a detailed implementation of the unedited electron-db that causes the update method to only work on the first record? I've used it in my apps and they work just fine. So I needed a little more detailed data. Thank you.

alexiusacademia commented 5 years ago

Bug fixed. Hi miladkian, I've fixed the bug issue in updateRow. By only moving the 2 lines of code you mentioned above will only fix the message. My mistake was that the whole setting and comparison of every matched key and value were inside the loop, hence the comparison runs at each loop.

Thank you for opening the issue, it helped fixed the problem.