[英]Why am I getting data from an empty mongodb database?
I'm doing a todo application, and in my code I'm using the ng-repeat
directive to create as many todoboxes as there are in the database. 我正在做一个待办事项应用程序,在我的代码中,我使用
ng-repeat
指令来创建与数据库中一样多的待办事项框。 But in my case even if the database is empty if get five todo boxes. 但就我而言,即使数据库为空,也要获得五个待办事项框。 It probably has something to do with my
loadData
function that I'm calling but I can't figure it out. 它可能与我正在调用的
loadData
函数有关,但我无法弄清楚。 Here is an image of what I get on my browser combined with the data from the debugger. 这是我在浏览器中得到的图像以及调试器中的数据的图像。 .
。 When I do a get request on localhost:3000/api/todos I get
[]
which is right because the database is empty. 当我在localhost:3000 / api / todos上执行get请求时,我得到
[]
是正确的,因为数据库为空。 Any ideas? 有任何想法吗?
Here is my part of my html
file: 这是我的
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>
Here is my 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);
});
}
}]);
and here is my server.js
file: 这是我的
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);
The $http
service returns a promise which resolves to a response object. $http
服务返回一个承诺,该承诺将解析为一个响应对象。 Data
is one of the five properties of the response object. Data
是响应对象的五个属性之一。 Set todos
to response.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.