gpt4 book ai didi

node.js - MEAN Stack CRUD todolist 给出无限列表,不会删除

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:50 25 4
gpt4 key购买 nike

我已经为此绞尽脑汁好几天了,但我仍然不确定出了什么问题。一旦加载本地主机,我就会得到无限的项目列表。这些元素通常是空的并且不包含任何东西。当我尝试删除它们时,我收到 404 错误,提示“api/todos/undefined”。

下面是代码。如果有很多,我很抱歉。我只是不知道错误可能出在哪里。也许是在命名中?肯定还存在路由问题。

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/');

var Todo = mongoose.model('todos', {
text : String
});

module.exports.Todo = Todo;

Controller

var baselTodo = angular.module('baselTodo', []);

function mainController($scope, $http) {
$scope.formData = {text: ''};

// when landing on the page, get all todos and show them
$http.get('/api/todos')
.success(function(data) {
$scope.todos = data;
})
.error(function(data) {
console.log('Error: ' + data);
});


// when submitting the add form, send the text to the node API
$scope.createTodo = function() {
$http.post('/api/todos', $scope.formData)
.success(function(data) {
$scope.todos = data;
})
.error(function(data) {
console.log('Error: ' + data);
});
$http.get('/api/todos').success(function(data) {
$scope.todos = data;
})
};

// delete a todo after checking it
$scope.deleteTodo = function(id) {
$http.delete('/api/todos/' + id)
.success(function(data) {
$scope.todos = data;
})
.error(function(data) {
console.log('Error: ' + data);
});
};

{
$scope.updateTodo = function(id) {
$scope.newItem = prompt("Please enter your new item: ", "");
$http.put('/api/todos/' + id, {text: $scope.newItem}).success(function(data) {
$scope.todos = data;
});

$http.get('/api/todos').success(function(data) {
$scope.todos = data;
});
}};




};

html

<body ng-controller="mainController">

<div class="container">
<div id="person-form">
<form>

<div class="form-group">
<h1>Enter Item:</h1>
<input type="text" ng-model="formData.text">
</div>

<button ng-click="createTodo()">Create</button>

<h3>Current List:</h3>
<ul ng-repeat="todo in todos">
<li> {{todo.text + " || ID: " + todo._id}} </li>
<button ng-click="updateTodo(todo._id)">Update</button>
<button ng-click="deleteTodo(todo._id)">Delete</button>
</ul>

</form>
</div>
</div>

</body>
</html>

服务器

// ================== SERVER.JS ========================

// set up ------------------------------------------
var api = require('./routes/api');
var express = require('express');
var app = express();

// configuration -----------------------------------
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
});

// listen (start app with node server.js) ----------
app.listen(3000);
console.log("App listening on port 3000");

// routing -----------------------------------------

// Main Page
app.get('/', function(req, res) {
res.sendfile('./public/index.html');
});

// API Routing
app.get('/api/users', api.read);
app.post('/api/users', api.create);
app.put('/api/users/:_id', api.update);
app.delete('/api/users/:_id', api.delete);

最佳答案

您的 server.js 文件中缺少分号。你想要这个:

app.put('/api/todos/:_id', api.update);

也给另一个添加一个分号。

关于node.js - MEAN Stack CRUD todolist 给出无限列表,不会删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com