javascript - 为什么我要从一个空的 mongodb 数据库中获取数据?

标签 javascript angularjs mongodb

我正在做一个待办事项应用程序,在我的代码中,我使用 ng-repeat 指令来创建与数据库中一样多的待办事项框。但在我的例子中,如果得到五个待办事项框,即使数据库是空的。它可能与我正在调用的 loadData 函数有关,但我无法弄清楚。 Here is an image of what I get on my browser combined with the data from the debugger. .当我在 localhost:3000/api/todos 上执行获取请求时,我得到 [] 这是正确的,因为数据库是空的。有什么想法吗?

这是我的 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>

这是我的 Controller :

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 服务返回一个解析为响应对象的 promise 。 Data 是响应对象的五个属性之一。将 todos 设置为 response.data

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

关于javascript - 为什么我要从一个空的 mongodb 数据库中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37001442/

相关文章:

mongodb - mongoDB 可以比较字段名而不是字段值吗?

javascript - ScrollReveal 在自定义视口(viewport)中不起作用

javascript - 当值为 false 时在 ng-bind-html 上显示图标

javascript - 在 Mongoose 之上创建一个额外的数据访问层

javascript - 如何获得带有选择/取消选择所有功能和不确定值的 angular.js 复选框?

javascript - Angular-modal-service 不显示模态

ruby-on-rails - Heroku 的 Rails、Mongoid 和 Unicorn 配置

javascript - 传递 <tr> id 值 javascript 函数

javascript - 为什么我不能制作这个工作代码的 jsFiddle?

javascript - sapui5 弹出窗口导航到另一个 xmlview