2017-10-11 50 views
0

我想通過說我在這裏閱讀了幾篇關於這個問題的文章來說明這一點。.insertOne不是函數

我有以下節點/快遞/蒙戈應用:

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) 
} 

我蒙戈架構文件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 is not a function

現在幾個在這裏堆的帖子已經告知,我要確保我出口從我的模型類,我做的模型,這將修復錯誤這個問題。它的奇怪,因爲findOne方法似乎很好,但當我打電話給insertOne我遇到了問題。

我在這裏錯過了什麼嗎?

回答

1

貓鼬模型沒有一個insertOne方法。使用create方法代替:

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

編輯

mongoose documentation,請嘗試使用

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

The Mongoose docs展示如何創建文件:

或者通過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) { 
    // ... 
}) 
0

insertOne命令在Mongoose Documentation中提到的直接不可用於貓鼬。如果你想使用insertOne命令,那麼你需要使用批量命令來發送這個命令到MongoDB服務器。像下面的東西。我希望這可以工作。

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