“TypeError: require(...).collection 不是函数”

时间:2021-08-01 10:47:37

标签: node.js mongodb

我正在尝试使用登录来构建此应用程序并创建新帐户功能等。该应用程序在 localhost:3000 上运行良好,但突然间它停止加载并且我在命令行中出现错误。< /p>

这里是 usercontroller.js

const User=require('../models/User')

exports.home=function(req,res){
    res.render('HOME')

}
exports.contact=function(req,res){
    res.render('CONTACT')
}
exports.about=function(req,res){
    res.send('tenks babes')
}
exports.register=function(req,res){
    let user = new User(req.body)
    user.register()
    if (user.errors.length){
        res.send(user.errors)
    }else{
        res.send('congrats no')
    }
    
}  

这是我的 User.js

const {usercollection}=require('../db').collection('users')
const validator=require('validator')
let User=function(data){
    this.data=data
    this.errors=[]
}

User.prototype.cleanUp=function(){
    if (typeof(this.data.uname)!='string'){this.data.uname=""}
    if (typeof(this.data.password)!='string'){this.data.password=""}
    if (typeof(this.data.email)!='string'){this.data.email=""}
}

/// ab hum ye chahty hain ke koi bhi apne pass se koi property na de to isko manually set krdegy apne data ko

//this.data={
    //uname:this.data.uname.trim().toLowerCase(),
    ///password:this.data.password,
    ///email:this.data.email.trim().toLowerCase()
    // }




User.prototype.validate = function(){
    if (this.data.uname == "" ){this.errors.push('you must be provided username babes')}
    if (this.data.password == "" ){this.errors.push('you must be provided password babes')}
    if (this.data.email == "" ){this.errors.push('you must be provided email babes')}
    if (this.data.password.length<8){this.errors.push('password must exceed 8 chars')}
    //if (this.data.uname.length>0 && this.data.uname.length<8){this.errors.push('password must exceed 8 chars')}
    if (!validator.isEmail(this.data.email)){this.errors.push('email should be properly added')}
    if (this.data.uname!="" && !validator.isAlphanumeric(this.data.uname)){this.errors.push('username always contain letters and number')}
    
}



User.prototype.register = function(){
    this.cleanUp()
    this.validate()

    if (!this.errors.length){
        usercollection.insertOne(this.data)
    }

}
module.exports=User



这是我的 db.js


const mongoose=require('mongoose')
const connectionString="mongodb+srv://user:<1234@987>@cluster0.1olct.mongodb.net/complexapp?retryWrites=true&w=majority"
mongoose.connect(connectionString,{useNewUrlParser:true,useUnifiedTopology:true},function(){
    module.exports=client.db()
    const app=require('./app')
    app.listen(3000)
})




显示的错误:

TypeError: require(...).collection is not a function
    at Object.<anonymous> (C:\Users\Bilal\Desktop\bilal2\models\User.js:1:41)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (C:\Users\Bilal\Desktop\bilal2\controller\usercontroller.js:1:12)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)

0 个答案:

没有答案