.insertOne 不是一个函数

.insertOne is not a function

提问人:mufc 提问时间:10/11/2017 最后编辑:3gthmufc 更新时间:6/12/2021 访问量:46949

问:

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

我有一个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架构文件帐户.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()
        })
      }
    })
  }
}

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

我收到错误

TypeError:Account.insertOne 不是函数

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

我在这里遗漏了什么吗?

JavaScript Node.js MongoDB

评论


答:

1赞 Roberto Bisello 10/11/2017 #1

编辑

Mongoose 文档中,请尝试使用

Account.create({ ...params ... }, function (err, small) {
  if (err) return handleError(err);
  // saved!
})
52赞 JohnnyHK 10/11/2017 #2

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

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

评论

0赞 Aaron Franke 10/31/2020
我以前使用 ,我注意到这将创建一个名为 .如何禁用此功能?updateOneupsertcreate__v
1赞 MasterMind 4/14/2021
@AaronFranke 这有帮助吗?stackoverflow.com/questions/13699784/mongoose-v-property-hide迟到总比不到好
8赞 TimoStaudinger 10/11/2017 #3

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

通过 Account.create()

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

或者通过实例化和 save() 来设置帐户:

new Account({email: req.body.email, password: req.body.password}).save(function (err, res) {
    // ...
})
1赞 Actung 10/11/2017 #4

insertOne命令在 Mongoose 中不可用,如 Mongoose 文档中所述。如果要使用命令,则需要使用批量命令才能将此命令发送到MongoDB服务器。如下所示。我希望这能奏效。insertOne

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