.insertOne 不是函数

2023-12-20

我想先说一下我已经阅读了这里有关此问题的几篇文章。

我有一个节点/express/mongo 应用程序,其中包含以下内容:

app.js:

    var express = require('express')
    var bodyParser = require('body-parser')
    var cors = require('cors')
    var morgan = require('morgan')
    var mongoose = require('mongoose')
    var passport = require('passport')

    var app = express()

    // MongoDB Setup
    var configDB = require('./config/database.js')
    mongoose.connect(configDB.url)

    app.use(morgan('combined'))
    app.use(bodyParser.json())
    // Check security with this
    app.use(cors())
     // load our routes and pass in our app and fully configured passport

    require('./routes')(app)
    app.listen(process.env.PORT || 8081)
    console.log('We are up and running, captain.')

路线.js

const AuthenticationController = require('./controllers/AuthenticationController')

module.exports = (app) => {
  app.post('/register', AuthenticationController.register)
}

我的 mongo 架构文件 Account.js:

const mongoose = require('mongoose')
const bcrypt = require('bcrypt-nodejs')
const Schema = mongoose.Schema

var accountSchema = new Schema({
  email: String,
  password: String,
  likesPerDay: { type: Number, min: 0, max: 250 },
  followPerDay: { type: Number, min: 0, max: 250 },
  unfollowPerDay: { type: Number, min: 0, max: 250 },
  commentsPerDay: { type: Number, min: 0, max: 250 },
  comment: String,
  hashtags: [String]
})

// methods ======================
// generating a hash. We hash password within user model, before it saves to DB.
accountSchema.methods.generateHash = function (password) {
  return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null)
}

// checking if password is valid
accountSchema.methods.validPassword = function (password) {
  return bcrypt.compareSync(password, this.local.password)
}

// create the model for users and expose it to our app
module.exports = mongoose.model('Account', accountSchema)

最后是我的控制器文件 AuthenticationController.js

const Account = require('../models/Account.js')
// var bodyParser = require('body-parser')

module.exports = {
  register (req, res) {
    Account.findOne({email: req.body.id}, function (err, account) {
      if (err) {
        console.log('Could not regster user')
        throw err
      }
      if (account) {
        console.log('account already exists')
      } else {
        Account.insertOne({email: req.body.email, password: req.body.password}, function (err, res) {
          if (err) {
            console.log('could not insert')
            throw err
          }
          console.log('inserted account')
          Account.close()
        })
      }
    })
  }
}

我的错误认证控制器当我打电话时归档Account.insertOne功能。

我得到的错误是

类型错误:Account.insertOne 不是函数

现在,堆栈上的几篇文章建议我确保从我的模型类中导出模型,我正在这样做,这将解决这个问题。这很奇怪,因为findOne方法似乎没问题,但是当我调用insertOne我遇到一个问题。

我在这里错过了什么吗?


Mongoose 模型没有insertOne方法。使用create http://mongoosejs.com/docs/api.html#model_Model.create方法改为:

Account.create({email: req.body.email, password: req.body.password}, function (err, doc) {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.insertOne 不是函数 的相关文章

随机推荐

  • 防止 Kivy 留下调试消息

    我有一个简单的 Kivy 界面 也使用终端 示例代码 import kivy kivy require 1 0 6 from kivy app import App from kivy uix label import Label clas
  • 如何在 vue cli 中使用 Moment.js

    我已经 npm 安装了 vue moment 我需要通过 for 循环将日期传递给 p 标记 还我需要创建一个方法我可以在其中添加天数到我的日期 以便它显示该天数之后的日期 我该怎么做或者我错在哪里 main js代码 Vue use re
  • 从另一个文件导入和更改变量

    Okay 我一直在寻找可以直接回答我的问题的答案 但没有成功 我的问题非常简单 老实说我认为会有更直接的答案 请记住 我对这门语言还比较陌生 并且仍在学习中 所以我会用fileA and fileB作为我的两个文件 以及x作为我的示例变量
  • “应用程序安装失败。发生运行时错误。修复 App Manifest.xml 文件中的功能。”

    我正在为 WP8 构建这个简单的应用程序 一切都工作正常 直到今天我必须向清单文件添加新功能以允许访问传感器 我只是用图形编辑器打勾ID CAP SENSORS 现在 每次我尝试部署我的应用程序时 我都会得到 Installation of
  • SQLSTATE[HY000] [2002] yii2 中没有这样的文件或目录

    I use ubuntu 16 04 PHP Version 7 0 4 7ubuntu2 Apache 2 4 18 Ubuntu PHP extension mysqli 在 phpmyadmin 中写的 我从升级我的ubuntu15
  • WifiP2pManager.discoverPeers 在 android 10 中失败

    以下代码示例返回Error code 0 这是android中内部错误的错误代码 是否有任何解决方法可以在 Android 10 设备中发现对等点 wifip2pmanager discoverPeers wifip2pmanagerCha
  • 如何将Int16Array缓冲区保存到wav文件节点js

    我在音频处理时将 Int16Array 缓冲区发送到服务器 var handleSuccess function stream globalStream stream input context createMediaStreamSourc
  • 将新行添加到 pandas 中的现有数据框时出现错误

    我有以下数据框 df3 pd DataFrame columns Devices months 我从循环中获取行值 排 打印 数据 Devices months 1 Powerbank Feb month 当我将此数据行添加到 df3 时
  • 如何将结构体中的数组传递给内核?

    我需要向内核输出一个结构数组 其中将有一个数组 但最终 数据有点正确 但在某种程度上存在错误 我在主机上有这个代码 struct myStruct int a double b double c 5 myStruct result new
  • ASP.NET - 验证 UserControl 内的控件

    我有一个围绕 DropDownList 的包装器 UserControl 用于管理 DropDownList 值的语言特定翻译 我还有一个内部下拉列表所需的必填字段验证器 如何通过用户控件公开此控件以允许验证 我目前收到的错误如下 cann
  • Firebase数据库使用Map获取子级的特定值

    问题 我正在创建一个允许用户预订办公桌的 Android 应用程序 但遇到了如何从 Firebase 实时数据库子值检索数据的问题 我的代码目标 我想获取所有用户下所有条目的用户 ID 和办公桌号码 以便我可以将已预订的办公桌设置为在我的页
  • 输入(浮点)在保存时转换为整数

    问题 当表单的信息发送到 POST 操作时 浮点变量会丢失小数点并转换为integer代替float 信息 这是包含变量定义的类 PUnit and CoinValue public class Device Key public int
  • 如何避免在更新接口后必须更新所有实现类?

    我们有一个 WPF 应用程序 它主要是一个图表应用程序 大约有 30 个图表 每个图表在数据库中都有自己的表 其中保存图表的配置信息 每个图表在应用程序中都有一个类 其中包含与图表相关的配置信息 我们的应用程序中有一个界面 IChartCo
  • android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback 中出现奇怪的 ArrayIndexOutOfBoundsException

    我在 Play Store 控制台中收到了一个与以下相关的奇怪的越界异常android support v4 app ActivityCompat OnRequestPermissionsResultCallback https devel
  • 在 RadGrid Telerik 中查找分页记录

    当启用分页时 如何在 radGrid 中通过键查找给定记录 插入元素后 我想选择新行 这就是我需要此功能的原因 谢谢你的帮助 一种方法是禁用分页 然后进行Rebind 然后迭代所有项目 找到该项目应位于的页面 然后启用分页 另一种方法是单独
  • Heroku 无法访问此站点 (DNS_PROBE_FINISHED_NXDOMAIN)

    我在 Heroku 上的一些个人应用程序和专业应用程序无法访问 它们没有链接的自定义域 因此我通过 http herokuapp com 访问它们 大多数时候 它们工作得很好 但我经常从浏览器收到以下响应 This site can t b
  • 读取 CPU 使用率时出错:/proc/stat(权限被拒绝)

    附件是运行应用程序后 Android Studio 中出现的错误消息 我能够在设备上构建并运行该程序 没有任何问题 直到我开始按照 Google 的新应用程序要求定位 API 级别 26 后 我才收到此错误 关于如何修复错误有什么建议吗 另
  • vsto 插件中的本地化

    这是在 VSTO Word Addin 中实现本地化的正确方法吗 现在 我只是遵循命名约定 将当前文化附加到它搜索的资源文件中 我已经阅读了足够多的不同材料 感到困惑 一种方法是使用 Resgen exe 然后 Al exe 生成 dll
  • 为什么在 Swing 中使用空布局会令人不悦?

    最近 我开始为我工作的公司创建一个程序 正如背景信息一样 我仍然是一名学生和初学者程序员 所以我的解决方案可能不被推荐 而且我不知道如何做到这一点 但它有效 我不会因此而受到评判 因为这是一项与编程完全无关的学生工作 该程序的问题是 它将在
  • .insertOne 不是函数

    我想先说一下我已经阅读了这里有关此问题的几篇文章 我有一个节点 express mongo 应用程序 其中包含以下内容 app js var express require express var bodyParser require bo