javascript - .insertOne 不是函数

标签 javascript node.js mongodb

我想先说一下,我已经阅读了这里的几篇关于这个问题的帖子。

我有一个 node/express/mongo 应用,包含以下内容:

应用程序.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)

最后是我的 Controller 文件 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 函数时,我的 AuthenticationController 文件出现错误。

我得到的错误是

TypeError: Account.insertOne is not a function

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

我是不是漏掉了什么?

最佳答案

Mongoose 模型没有 insertOne 方法。使用 create方法代替:

Account.create({email: req.body.email, password: req.body.password}, function (err, doc) {

关于javascript - .insertOne 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46692306/

相关文章:

javascript - node.js 中 STARTTLS 的特定 TLS 版本

node.js - nodejs npm 抛出错误

javascript - es6 导出的对象值未更新?

javascript - 通过点击数组元素来删除它(JS)

javascript - 如何将字段值合并到组中?

javascript - Q.nfcall 和 Q.fcall 有什么区别?

c# - 将 ExpandoObject 持久化到 MongoDB

node.js - 如何在 ReactJS 中显示来自 MongoDB 的图像

mongodb - 使用 Spring Data mongo Query 删除集合中的所有文档

javascript - 使用 jQuery 将属性从一个元素复制到另一个元素