Mongoose.js - TypeError:Model.deleteOne 不是函数 [英] Mongoose.js - TypeError: Model.deleteOne is not a function

查看:68
本文介绍了Mongoose.js - TypeError:Model.deleteOne 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 node.js 的新手,因此这是一个非常简单的问题.

我有以下几行代码:

(A) 定义模型

var mongoose = require('mongoose');var 类型架构 = mongoose.Schema({名称:{类型:字符串,要求:真实}});

执行模型删除操作

module.exports.deleteGenre = function (id, callback) {console.log("删除流派:"+ id);var 条件 = {"_id": id};Genre.deleteOne(条件,回调);};

我参考了以下关于函数的链接.>

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

TypeError: Genre.deleteOne 不是函数

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

解决方案

正如 user3344977 在评论中所说,函数 deleteOnedeleteMany 在 mongoose 4 中不再存在.API 文档不是最新的.

我刚刚查看了 mongoose (4.4) 的源代码,没有发现这些功能的踪迹.

您可以使用 Model.findOneAndRemove(condition, options, callback)Model.findByIdAndRemove(id, options, callback) 代替.

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

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.

解决方案

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

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

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

这篇关于Mongoose.js - TypeError:Model.deleteOne 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆