.insertOne不是一个函数

muf*_*ufc 3 javascript mongodb node.js

我想在此前言,我已经在这里阅读了几个关于这个问题的帖子.

我有一个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.')
Run Code Online (Sandbox Code Playgroud)

routes.js

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

module.exports = (app) => {
  app.post('/register', AuthenticationController.register)
}
Run Code Online (Sandbox Code Playgroud)

我的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)
Run Code Online (Sandbox Code Playgroud)

最后我的控制器文件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()
        })
      }
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

当我调用函数时,我的AuthenticationController文件中出现错误Account.insertOne.

我得到了错误

TypeError:Account.insertOne不是函数

现在堆栈上的几个帖子已经建议我确保我从模型类中导出模型,我正在做,这将解决这个问题.它很奇怪,因为这个findOne方法看起来很好,但是当我打电话给insertOne我时会遇到问题.

我在这里错过了什么吗?

Joh*_*yHK 11

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

Account.create({email: req.body.email, password: req.body.password}, function (err, doc) {
Run Code Online (Sandbox Code Playgroud)


Tim*_*imo 5

Mongoose 文档展示了如何创建文档:

要么通过Account.create()

Account.create({email: req.body.email, password: req.body.password}, function (err, res) {
    // ...
})
Run Code Online (Sandbox Code Playgroud)

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

new Account({email: req.body.email, password: req.body.password}).save(function (err, res) {
    // ...
})
Run Code Online (Sandbox Code Playgroud)