2016-12-04 31 views
0

私はto-doリストを作成する必要があり、最初のバージョンは正常に機能しました(データベース接続なし)。node.js sqliteデータベース接続(フロントエンドangular.js)

index.htmlを

<div id="todoCard" ng-repeat="task in taskList"> 
       <div id="label"> 
        <label id="description" class="description" ng-class="{strike: task.done}"> 
         <input type="checkbox" ng-model="task.done"/> 
         {{task.description}} 

         <table id="buttons"> 
          <tr> 
           <td> 
            <button id="edit" ng-click="editTodo()">Edit</button> 
           </td> 
           <td> 
            <button id="del" ng-click="deleteTodo($index)">Del</button> 
           </td> 
          </tr> 
         </table> 
       </div> 
      </div> 

controller.js

'use strict'; 
angular.module('toDoApp.controller',[]) 
.controller('toDoController',["$scope", function($scope){ 


    $scope.newTask = ""; 
    $scope.taskList = [ 
     {description: "Buy airplane tickets", done:false}, 
     {description: "Make hotel reservation", done:false}, 
     {description: "Lorem Ipsum", done:true} 
    ]; 

    $scope.addTodo = function() { 
     $scope.taskList.push({description: $scope.newTask, done:false}); 
     $scope.newTask = ""; 
    } 

    $scope.deleteTodo = function(index) { 
     $scope.taskList.splice(index, 1); 

    } 

}]); 

しかし、その後、私はSQLiteのデータベースに接続し、データベースからカードを見せたかったです。

factory.js

'use strict'; 
angular 
    .module('toDoApp') 
    .factory('toDoAppFactory', function(){ 
     var sqlite3  = require('sqlite3').verbose(); 
     var fs   = require('fs'); 

     // Setup database: 
     var dbFile = 'database.db'; 
     var dbExists = fs.existsSync(dbFile); 

     // If the database doesn't exist, create a new file: 
     if(!dbExists) 
     { 
      fs.openSync(dbFile, 'w'); 
     } 

     // Initialize the database: 
     var db = new sqlite3.Database(dbFile); 

     // Optional installation for newly created databases: 
     if(!dbExists) 
     { 
      db.run('CREATE TABLE `todocards` (' + 
      '`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,' + 
      '`created` TEXT NOT NULL,' + 
      '`deadline` TEXT NOT NULL,' + 
      '`done` INTEGER NOT NULL,' + 
      '`description` TEXT NOT NULL)'); 
     } 

     // Insert some data using a statement: 
     var statement = db.prepare('INSERT INTO `todocards` (`created`, `deadline`, `done`, `description`) ' + 
     'VALUES (?, ?, ?, ?)'); 
     statement.run('2016-05-09', '2016-05-12', Math.round(Math.random() * 1), 'Ipsum'); 
     statement.finalize(); 

     db.each("SELECT * FROM `todocards`", function (err, row) { 
      var taskList = JSON.stringify(row); 

      }); 

     function getCards(taskList) { 
       return taskList; 
      } 
      return { 
      getCards: getCards 
      } 
db.close(); 


    }); 

controllers.jsがに変更されました:

'use strict'; 
angular.module('toDoApp.controller',[]) 
.controller('toDoController',["$scope", function($scope, toDoAppFactory){ 


    $scope.newTask = {}; 
    $scope.taskList = toDoAppFactory.getCards(); 





}]); 

factory.js Iをindex.htmlにするために含まれている。ここ

接続がありますconsole.logでfactory.jsをテストしたところ、var taskList = JSON.stringify(row);に配列があります。しかし、何か間違っている。

この問題を解決してください。

答えて

0

私はこのバリアントをデバッグfactory.js

var taskList = [];// create and array outside to store your rows 
//.each iterates over each row. so push them in array array 
db.each("SELECT * FROM `todocards`", function (err, row) { 
    taskList.push(JSON.stringify(row)); 
}); 

function getCards() { //dont pass in any parameters here 
    console.log(taskList); //just a small check.. 
    return taskList; 
} 
+0

でそう

..あなたは、テーブルとデータ・レディを持っていると仮定するつもりです:このバリアントをデバッグ – GEZ

+0

:行がテーブルにプッシュされますが、 console.log(taskList)は何も表示しません... – GEZ

関連する問題