【问题标题】:.insertOne is not a function.insertOne 不是函数
【发布时间】:2018-03-23 08:52:48
【问题描述】:

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

我有一个 node/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.')

routes.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 函数时,我的 AuthenticationController 文件中出现错误。

我得到的错误是

TypeError: Account.insertOne 不是函数

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

我错过了什么吗?

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

    编辑

    截至mongoose documentation,尝试使用

    Account.create({ ...params ... }, function (err, small) {
      if (err) return handleError(err);
      // saved!
    })
    

    【讨论】:

      【解决方案2】:

      Mongoose 模型没有insertOne 方法。请改用create 方法:

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

      【讨论】:

      【解决方案3】:

      The Mongoose docs 展示如何创建文档:

      通过Account.create()

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

      或者通过实例化和save()ing帐户:

      new Account({email: req.body.email, password: req.body.password}).save(function (err, res) {
          // ...
      })
      

      【讨论】:

        【解决方案4】:

        insertOne 命令在 mongoose 中不可用,如Mongoose Documentation 中所述。如果您想使用insertOne 命令,那么您需要使用批量命令才能将此命令发送到 MongoDB 服务器。像下面的东西。我希望这行得通。

        Account.bulkWrite([
          {
            insertOne: {
              document: {email: req.body.email, password: req.body.password}
            }
          }
        }]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-03
          • 2019-02-04
          • 1970-01-01
          • 1970-01-01
          • 2017-05-06
          • 2019-12-20
          • 1970-01-01
          相关资源
          最近更新 更多