簡體   English   中英

為什么我要從一個空的mongodb數據庫中獲取數據?

[英]Why am I getting data from an empty mongodb database?

我正在做一個待辦事項應用程序,在我的代碼中,我使用ng-repeat指令來創建與數據庫中一樣多的待辦事項框。 但就我而言,即使數據庫為空,也要獲得五個待辦事項框。 它可能與我正在調用的loadData函數有關,但我無法弄清楚。 這是我在瀏覽器中得到的圖像以及調試器中的數據的圖像。 當我在localhost:3000 / api / todos上執行get請求時,我得到[]是正確的,因為數據庫為空。 有任何想法嗎?

這是我的html文件的一部分:

<div class="row">
  <h1>Things you have to do</h1>
  <li ng-repeat="todo in todos">
    <p id="todobox"> {{ todo.name }} </p>
  </li>
  <form>
    <input type="text" placeholder="I need to do..." ng-model="formData.newTodo" required>
    <button ng-click="addTodo(formData)">Add</button>
  </form>
</div>

這是我的控制器:

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

app.controller('MainController', ['$scope','$http', function($scope, $http){
  $scope.formData = {};

$scope.loadData = function(){
  $http.get('/api/todos')
    .then(function(data){
      console.log('Data:' + data);
      $scope.todos = data;
    });
};
//call the loadData function that returns the data from the json file
$scope.loadData();

//Add a new todo to the database
$scope.addTodo = function(){
  $scope.formData = {
    name: $scope.formData.newTodo
  };
  $http.post('/api/todos', $scope.formData)
    .then(function(data){
      console.log($scope.formData.name);
      $scope.todos = data;
      $scope.formData = {};
      console.log(data);
    });
}

}]);

這是我的server.js文件:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

//connect to database
mongoose.connect('mongodb://localhost:27017/tododbtest');

// set static files location
// used for requests that our frontend will make
app.use(express.static(__dirname + '/public'));

//app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//define our model for the todos
var Todo = mongoose.model('Todo', {
  name: String,
  //maybe change it to bool later
  //checked: boolean
});

//when I get a get request I'm going to send
//the index.html file
app.get('/', function(req, res){
  res.sendFile( __dirname + '/public/index.html');
});

//get all the todos from the database
app.get('/api/todos', function(req, res){
  Todo.find(function(err, todos){
    if(err)
      res.send(err)
    console.log(todos);
    res.json(todos);
  });
});

//create a todo
app.post('/api/todos', function(req, res){
  console.log("Req.body.name:" + req.body.name);
  Todo.create({name: req.body.name, checked: false},
  function(err, todo){
    if(err) res.send(err);
    Todo.find(function(err, todos){
      if(err) res.send(err);
      res.json(todos);
    });
  });
});

app.listen(3000);

$http服務返回一個承諾,該承諾將解析為一個響應對象。 Data是響應對象的五個屬性之一。 todos設置為response.data

  $http.post('/api/todos', $scope.formData)
    .then(function(response){
      console.log($scope.formData.name);
      $scope.todos = response.data;
      $scope.formData = {};
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM