http.put没有在nodejs app中更新正确的数据库列

时间:2015-08-10 05:20:34

标签: angularjs node.js express

我有一个数据库[| id | text(string)|完整(布尔值)],
我正在尝试将最后一列更新为true / false,具体取决于所选的操作。但我的代码是将文本列更新为true / false table view

html代码

<div class="container">
  <div class="header">
    <h1> Table </h1>
  </div>
  <div ng-app="nodeTodo" ng-controller="mainController">

    <table>
      <tr ng-repeat="x in todoData">
        <td>{{ x.id }}</td>
        <td>{{ x.text }}</td>
        <td>{{ x.complete }}</td>
        <td>
          <div class="btn-group">
            <button type="button" class="btn btn-danger">Action</button>
            <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
              <span class="caret"></span>
              <span class="sr-only">Toggle Dropdown</span>
            </button>
            <ul class="dropdown-menu" role="menu">
<li ng-repeat="choice in actions">
<a ng-click="updateAssign(x.id, x.text, choice)"> {{choice}} </a>
</li>
            </ul>
          </div>
        </td>
      </tr>
    </table>
  </div>
</div>

mainController

angular.module('nodeTodo', [])

.controller('mainController', function($scope, $http) {

$scope.formData = {};
$scope.todoData = {};
$scope.actions = [
"true",
"false"
];
$scope.action = false;


// Get all todos
$http.get('/api/v1/todos')
    .success(function(data) {
        $scope.todoData = data;
        console.log(data);
    })
    .error(function(error) {
        console.log('Error: ' + error);
    });

// Create a new todo
$scope.createTodo = function(todoID) {
    $http.post('/api/v1/todos', $scope.formData)
        .success(function(data) {
            $scope.formData = {};
            $scope.todoData = data;
            console.log(data);
        })
        .error(function(error) {
            console.log('Error: ' + error);
        });
};

// Delete a todo
$scope.deleteTodo = function(todoID) {
    $http.delete('/api/v1/todos/' + todoID)
        .success(function(data) {
               $scope.todoData = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};

// update assignment
$scope.updateAssign = function(todoID, textOld, choice) {
    if (String(choice) == "true") {
        var boolchoice = true;
    }
    else {
        var boolchoice = false;
    }
    console.log('Debug1:' + textOld + ' ' + boolchoice);
    $http.put('/api/v1/todos/' + todoID, {text: textOld, complete: boolchoice})
        .success(function(data) {
            $scope.todoData = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};

});  

Router.put

router.put('/api/v1/todos/:todo_id', function(req, res) {
var results = [];
// Grab data from the URL parameters
var id = req.params.todo_id;
// Grab data from http request
var data = {text: req.body.text, complete: req.body.complete};
console.log("Debug:" + data.text + " " + data.complete);
// Get a Postgres client from the connection pool
pg.connect(connectionString, function(err, client, done) {

    // SQL Query > Update Data
    client.query("UPDATE items SET text=($1), complete=($2) WHERE id=($3)",  [data.text, data.complete, id]);
    // SQL Query > Select Data
    var query = client.query("SELECT * FROM items ORDER BY id ASC");
    // Stream results back one row at a time
    query.on('row', function(row) {
        results.push(row);
    });

    // After all data is returned, close connection and return results
    query.on('end', function() {
        client.end();
        return res.json(results);
    });

    // Handle Errors
    if(err) {
      console.log(err);
    }

});

作为一个更好的方法在http.put中使用$ scope.var进行更新,但我不知道该怎么做。 我已经开始将此tutorial作为基点。

1 个答案:

答案 0 :(得分:0)

经过一番努力使其成功。将范围变量传递给router.put工作。

控制器中的更改

$scope.updateAssign = function(todoID, text, choice) {
if (String(choice) == "true") {
    $scope.action = true;
}
else {
    $scope.action = false;
}  
//  console.log('Debug1:' + text + ' ' + choice); 
    $http.put('/api/v1/todos/' + todoID, {text: text, complete: $scope.action})
        .success(function(data) {
            $scope.todoData = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};