猫鼬。js - TypeError: Model.deleteOne不是函数

时间:2021-12-10 15:46:12

I am new to node.js, hence a very trivial question.

我是node的新手。js,这是一个很简单的问题。

I have the following lines of code:

我有以下几行代码:

(A) Defined a model

(一)定义了一个模型

var mongoose = require('mongoose');
var genreSchema = mongoose.Schema({
  name:{
    type: String,
    required: true
  }
});

Perform model delete operation

模型执行删除操作

module.exports.deleteGenre = function (id, callback) {
  console.log("Deleting genre: "+ id);
  var condition = {"_id": id};
  Genre.deleteOne(condition, callback);
};

I have referred to the following link on the function.

我已经提到了关于这个函数的以下链接。

However, when I run it, it throws the following error:

但是,当我运行它时,它会抛出以下错误:

TypeError: Genre.deleteOne is not a function

If I replace the deleteOne() function with remove(), the code works. Please advise, where am I going wrong.

如果我用remove()替换deleteOne()函数,代码就可以工作了。请告知我哪里出错了。

1 个解决方案

#1


3  

as user3344977 said in comment, the function deleteOne and deleteMany no longer exist in mongoose 4. the API documentation is not up to date.

user3344977在评论中说,在mongoose 4中,deleteOne和deleteMany函数已经不复存在。API文档不是最新的。

I just checked the source code of mongoose (4.4) and there is no a single trace of these function.

我刚刚检查了mongoose(4.4)的源代码,这些函数没有任何痕迹。

you can use Model.findOneAndRemove(condition, options, callback) or Model.findByIdAndRemove(id, options, callback) instead.

您可以使用模型。findOneAndRemove(条件、选项、回调)或模型。findByIdAndRemove(id、选择回调)。

#1


3  

as user3344977 said in comment, the function deleteOne and deleteMany no longer exist in mongoose 4. the API documentation is not up to date.

user3344977在评论中说,在mongoose 4中,deleteOne和deleteMany函数已经不复存在。API文档不是最新的。

I just checked the source code of mongoose (4.4) and there is no a single trace of these function.

我刚刚检查了mongoose(4.4)的源代码,这些函数没有任何痕迹。

you can use Model.findOneAndRemove(condition, options, callback) or Model.findByIdAndRemove(id, options, callback) instead.

您可以使用模型。findOneAndRemove(条件、选项、回调)或模型。findByIdAndRemove(id、选择回调)。