carol1287
carol1287

Reputation: 395

Object #<Scope> Error

I am calling the following function in my controller:

              try
              {
                  alert('it works!');
                  $scope.insert('John', 'Doe');
              }
              catch(err)
              {
                  alert('err=' + err);
              }

 $scope.insert = function(firstname, lastname) {
        var query = "INSERT INTO people (firstname, lastname) VALUES (?,?)";
        $cordovaSQLite.execute(db, query, [firstname, lastname]).then(function(res) {
            console.log("INSERT ID -> " + res.insertId);
        }, function (err) {
            console.error(err);
        });
    }

When I run it, sometimes it works and adds the data and sometimes I get the following error:

err=TypeError: Object #<Scope> has no method 'insert'

I have seen this error is quite common but I didn't find an answer that applies to this sample as it actually sometimes the insert works.

Upvotes: 0

Views: 141

Answers (2)

Rebornix
Rebornix

Reputation: 5270

Since you are using expressions to declare functions, $scope.insert() is called before it's defined, so you need to move function insert definition before try catch block.

However if you define you insert function as

function insert(firstName, lastName) { ... }

function insert would be defined before try catch block even we don't change its sequence.

Upvotes: 1

carol1287
carol1287

Reputation: 395

It seems I just moved $scope.insert('John', 'Doe'); after declaring and now i don't get the error anymore :). That was an easy fix!

Upvotes: 0

Related Questions