$https delete 在 nodejs 和 mongodb 中不起作用,无法删除
$https delete is not working in nodejs and mongodb ,not able to delete
id 我进入了控制器,但它进入了服务器,当我点击删除时,我在控制台中获得了 id。它显示错误 "possibly unhandled rejection" 和错误 404
这是我的服务器:-
var express=require('express');
var mongojs=require('mongojs');
var bodyParser=require('body-parser');
var app=express();
var db=mongojs('contactlist',['contactlist']);
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
app.get('/Contactlist',function (req,res) {
console.log("ireceived a get request")
db.contactlist.find(function (err,docs) {
console.log(docs);
res.json(docs);
})
})
app.post('/Contactlist',function (req,res) {
db.contactlist.insert(req.body,function (err,doc) {
res.json(doc);
console.log(doc);
})
})
app.delete('/contactlist/:id',function (req,res) {
var id= req.params.id;
console.log(id);
})
app.listen(3000);
console.log('Server running on port 3000');
这是我的控制器
var ContactListApp = angular.module('ContactListApp',[]);
ContactListApp.controller('AppCtrl',[ '$scope','$http',function($scope,$http) {
console.log("controller");
var refresh=function () {
$http.get('/Contactlist').then(success,error)
function success(response) {
console.log(response,"I got the data i requested")
$scope.Contactlist=response.data;
}
function error(response){
alert("Please check your code");
}
};
refresh();
$scope.addContact=function () {
console.log($scope.contact);
$http.post('/Contactlist',$scope.contact).then(success,error)
function success(response) {
console.log(response);
$scope.Contactlist=response.data;
refresh();
};
function error() {
alert('error occured');
}
$scope.contact =null;
}
$scope.remove=function (id) {
console.log(id);
$http.delete('/contactlist/',id);
refresh();
}
}]);
如果您在这里获取 ID
var id= req.params.id;
console.log(id);
然后简单地运行查询
db.contactlist.remove({_id:id},function (err,doc) {
if(err){ console.log("err n remove")}
else{console.log("Remove Success")}
})
我想你应该这样称呼它
$http.delete('/contactlist/'+id);
id 我进入了控制器,但它进入了服务器,当我点击删除时,我在控制台中获得了 id。它显示错误 "possibly unhandled rejection" 和错误 404
这是我的服务器:-
var express=require('express');
var mongojs=require('mongojs');
var bodyParser=require('body-parser');
var app=express();
var db=mongojs('contactlist',['contactlist']);
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
app.get('/Contactlist',function (req,res) {
console.log("ireceived a get request")
db.contactlist.find(function (err,docs) {
console.log(docs);
res.json(docs);
})
})
app.post('/Contactlist',function (req,res) {
db.contactlist.insert(req.body,function (err,doc) {
res.json(doc);
console.log(doc);
})
})
app.delete('/contactlist/:id',function (req,res) {
var id= req.params.id;
console.log(id);
})
app.listen(3000);
console.log('Server running on port 3000');
这是我的控制器
var ContactListApp = angular.module('ContactListApp',[]);
ContactListApp.controller('AppCtrl',[ '$scope','$http',function($scope,$http) {
console.log("controller");
var refresh=function () {
$http.get('/Contactlist').then(success,error)
function success(response) {
console.log(response,"I got the data i requested")
$scope.Contactlist=response.data;
}
function error(response){
alert("Please check your code");
}
};
refresh();
$scope.addContact=function () {
console.log($scope.contact);
$http.post('/Contactlist',$scope.contact).then(success,error)
function success(response) {
console.log(response);
$scope.Contactlist=response.data;
refresh();
};
function error() {
alert('error occured');
}
$scope.contact =null;
}
$scope.remove=function (id) {
console.log(id);
$http.delete('/contactlist/',id);
refresh();
}
}]);
如果您在这里获取 ID
var id= req.params.id;
console.log(id);
然后简单地运行查询
db.contactlist.remove({_id:id},function (err,doc) {
if(err){ console.log("err n remove")}
else{console.log("Remove Success")}
})
我想你应该这样称呼它
$http.delete('/contactlist/'+id);