1

I'm trying to retrieve a list of data from mysql database by using electron and bind it to a list in the controllers scope. I'm using mysql2. Here is my controller:

$scope.carList = [];
mysql.execute("SELECT * FROM cars").spread(function(results){
    $scope.carList = results;
    console.log(results);
})

I do get the results back, but the in the view carList remains empty. How can I solve this problem?


I just added a button to my view and bound it to a check function like this:

$scope.check = function(){
    console.log($scope.carList);
}

After I click on the button, my list in the views gets populated. Now my question would be how can I have my list populated on the start of the controller rather than wait for an event ro make it happen?

7
  • 1
    How can you access your msql database via client application directly? Commented Mar 1, 2018 at 12:35
  • @lin, I'm using electron to build a desktop app. I just added this information to the question Commented Mar 1, 2018 at 12:42
  • You're trying to run a node module on the client and access your database from there. You can't do that, it's a node module and user's shouldn't be able to do whatever they want with your DB.. think about it! Commented Mar 1, 2018 at 12:51
  • @DominicTobias nope he is right. Please check electronjs.org/docs Commented Mar 1, 2018 at 12:51
  • 1
    @Dominic Tobias, For now I'm onlny getting a list of cars from local database. Later on, I will use a remote database, so your comment is appreciated. Commented Mar 1, 2018 at 13:59

1 Answer 1

1

I think mysql.execute("").spread(fn) promise is not a part of the AngularJS digest cycle. You did not provide enough code to fully reproduce your problem but I think by triggering a new digest cycle it should work for you. E.g. try it with $timeout which triggers a new digest cycle.

$scope.carList = [];

mysql.execute("SELECT * FROM cars").spread(function(results){
     $timeout(function () {
        $scope.carList = results;
     });
})

I would prefer to create a AngularJS service which handles your electron mysql in a nice way. You could globally apply your $scopes in it, right after finishing your mysql procedures which are not a part of your digest cycle.


Approach by using AngularJS promises

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope, $q) {


  $scope.carList = [];

  getCars.then(function(cars) {
    $scope.carList = cars;
  });


  function getCars() {

    var deferred = $q.defer();

    mysql.execute("SELECT * FROM cars").spread(function(results) {
      deferred.resolve(results);
    });

    return deferred.promise;
  }

});
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I do want to create a service only for database procedures. and your answer was quite helpful. Thank you
I'm getting an error that getCars.then is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.