簡體   English   中英

刪除進入Mongo的條目

[英]Deleting Entry to Mongo

我在從mongo DB刪除條目時遇到一些問題。 我正在使用node-mongodb-native

有問題的代碼是

ArticleProvider.prototype.delete = function(id, callback) {
    this.getCollection(function(error, article_collection) {
      if( error ) callback(error)
      else {
        article_collection.findAndRemove({_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
          if( error ) callback(error)
          else callback(null, result)
        });
      }
    });
};

這是一個奇怪的問題,因為我有一個函數可以返回單個文章

ArticleProvider.prototype.findById = function(id, callback) {
    this.getCollection(function(error, article_collection) {
      if( error ) callback(error)
      else {
        article_collection.findOne({_id: article_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
          if( error ) callback(error)
          else callback(null, result)
        });
      }
    });
};

就像魅力一樣

這是我的錯誤

500 TypeError: Cannot read property 'length' of undefined
at Function.createFromHexString (/Users/username/express_blog/node_modules/mongodb/lib/mongodb/bson/objectid.js:226:22)

這似乎是id類型的問題(或似乎)。

您傳遞的id參數必須是未定義的。

這是該函數當前版本的源代碼,或者是我在github上可以找到的最新版本的源代碼。

請注意,這里的框架代碼無法處理(typeof hexString === 'undefined')

ObjectID.createFromHexString = function createFromHexString (hexString) {
  // Throw an error if it's not a valid setup
  if(hexString != null && hexString.length != 24) throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format");

  var len = hexString.length;

  if(len > 12*2) {
    throw new Error('Id cannot be longer than 12 bytes');
  }

  var result = ''
    , string
    , number;

  for (var index = 0; index < len; index += 2) {
    string = hexString.substr(index, 2);
    number = parseInt(string, 16);
    result += BinaryParser.fromByte(number);
  }

  return new ObjectID(result);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM