为什么我从一个空的mongodb数据库中获取数据
Why am I getting data from an empty mongodb database?
我正在做一个todo应用程序,在我的代码中,我使用ng-repeat
指令来创建数据库中尽可能多的todobox。但在我的情况下,即使数据库是空的,如果有五个todo框。这可能与我正在调用的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
服务返回一个promise,该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 = {};
});
相关文章:
- 我可以使用Python和NodeJS中相同的MongoDB数据库吗
- 为什么我从一个空的mongodb数据库中获取数据
- 如何从Meteor内部获取当前连接的MongoDB数据库的URL
- 将 .exr 高度图解析为 mongoDB 数据库
- 两个服务器可以访问一个公共的mongoDB数据库吗?
- 使用Meteor.js使用多个Mongodb数据库
- 如何让变量保存 mongodb 数据库
- 如何使用NodeJS驱动程序创建新的MongoDb数据库
- 正在打开MongoDB数据库
- 可以't使用backbone.js从mongodb数据库中删除记录
- 仅在完成两个MongoDB数据库查询时呈现页面
- 从node.js向mongoDB数据库中插入数据
- 在Nodejs中,何时关闭MongoDB数据库连接
- 不能使用Meteor-Blaze访问mongodb数据库
- Node JS读取文件插入到mongodb数据库
- 从整个mongodb数据库中删除匹配的文档
- Id在mongodb数据库中保存为[object]
- 如何种子MongoDB数据库没有得到'null''s
- 使用mongodb数据库处理节点中的get请求数据
- mongoDB数据库分析器查询和getmore选项