2016-12-04 8 views
0

Мне нужно создать список дел, и первая версия работала хорошо (без подключения к базе данных).Соединение с базой данных node.js sqlite (frontend 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 был включен в index.html I 've протестировал factory.js с console.log, 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

Debugging этот вариант: строки помещаются в таблицу, но console.log (taskList) ничего не показывает ... – GEZ