【问题标题】:Can't delete a document from Mongodb via Express route无法通过 Express 路由从 Mongodb 中删除文档
【发布时间】:2016-07-27 12:32:15
【问题描述】:

我想通过 id 删除一个 Mongodb 文档,将它传递给 Express 路由。
在控制台中,我收到一条消息说它已被删除。

GET /api/videolinks 304 94.792 ms - -
Removed id= 562b905f633288ac0d8b4567
DELETE /api/videolinks/562b905f633288ac0d8b4567 200 68.550 ms - 19743

但事实并非如此。

> db.hyperlinks.find({"_id": ObjectId("562b905f633288ac0d8b4567")})
{ "_id" : ObjectId("562b905f633288ac0d8b4567"), "file" : "http://storage.akamai.com/get/b113/p/coub/simple/cw_file/79632d71313/9aedca2cd4d3094e75834/iphone_hellosergii_iphone.mp4" }

我的 Angularjs 工厂:

/*global angular*/
angular.module('myService', [])

    // each function returns a promise object 
    .factory('Videolinks', ['$http',function($http) {
        return {
            get : function() {
                return $http.get('/api/videolinks');
            },
            delete : function(id) {
                return $http.delete('/api/videolinks/' + id);
            }
        };
    }]);

我的路由.js

var path = require('path');
var Videolink = require('./models/mydb');

var mongodb = require('mongodb');

// Get links
function getLinks(res){
  Videolink.find(function(err, hyperlinks) {
    // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    if (err) {
        res.send(err);
    }

    res.json(hyperlinks); // return all videos in JSON format
  });
}

module.exports = function(app) {

    // api ---------------------------------------------------------------------

  // use mongoose to get all videos in the database
    app.get('/api/videolinks', function(req, res) {
        getLinks(res);
    });

    // delete a video
    app.delete('/api/videolinks/:video_id', function(req, res) {
        Videolink.remove({
            _id : mongodb.ObjectID(req.params.video_id)
        }, function(err) {

            if (err) {
                res.send(err);
            }
            console.log("Removed id= " + req.params.video_id);

            getLinks(res);
        });
    });


    // application -------------------------------------------------------------
    app.get('*', function(res) {
        res.sendFile('index.html', {root: path.join(__dirname, './public')}); // load the single view file 
    });
};

app.get 功能在这里运行良好。
app.delete 有什么问题?

这是我在models/mydb 中的数据库架构

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var db_schema = new Schema({
  //text: String
  _id: String,
  source: String,
  orig_page: String,
  likes: Number,
  title: String,
  file: String,
  video_mobile_res: String,
  video_high_res_mutes_muted: String,
  audio_high_res: String,
  video_med_res_muted: String,
  audio_med_res: String
}, {collection: 'hyperlinks'});

module.exports = mongoose.model('Videolink', db_schema);

【问题讨论】:

  • 只是您记录了它已被删除到控制台并没有做到这一点。你在用猫鼬吗?
  • 是的,我用猫鼬。
  • 您是否为该应用使用了任何类型的代理。我知道删除动词有时不会作为有效动词启用,需要被允许。
  • @RobertMoskal,我用数据库架构更新了我的问题。

标签: javascript node.js express mongoose mongoose-schema


【解决方案1】:

您的特殊问题是您在架构中将 _id 字段定义为字符串:

var db_schema = new Schema({
   _id: String,
   ...

去掉它,您的代码应该可以正常工作。您甚至可能发现了一个 mongoose 错误,因为您应该能够指定 _id 字段类型。也许一些猫鼬专家可以告诉我们更多。

【讨论】:

  • 我改成了Videolink.remove({_id : req.params.video_id}...,但是没有用。
  • 命令db.hyperlinks.find({"_id": ObjectId("562b905f633288ac0d8b4567")}) 返回文档。而db.hyperlinks.find({"_id": "562b905f633288ac0d8b4567"}) 没有给出任何结果。似乎 id 毕竟是一个对象。
  • 取出架构协程器中的_id:String。看看能不能解决。然后你可以尝试 findByIdAndRemove 应该能够强制字符串。
  • 我从数据库架构中删除了_id: String。现在它起作用了,文件被删除了。谢谢。
  • 我从未见过指定的 _id 字段类型。它必须引入这种奇怪的行为。我会更新我的答案,它可能会帮助其他人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多