简体   繁体   English

类型错误:newuser.create 不是 function

[英]TypeError: newuser.create is not a function

I am having issues trying to add a user from a form to my mongodb. I am using node and express and have required those in my js file.我在尝试将用户从表单添加到我的 mongodb 时遇到问题。我正在使用 node 和 express,并且在我的 js 文件中需要它们。 When I submit the from I get a newUser.create is not a function. In my index.js file I have:当我提交时,我得到一个 newUser.create 不是 function。在我的 index.js 文件中,我有:

const newUser = require('./models/newUser')
// mongoose.connect('mongodb://localhost/UAW', {useNewUrlParser: true, useUnifiedTopology: true})
mongoose.connect('mongodb://localhost/UAW', {useNewUrlParser: true})
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}));

And in my route in index.js is在 index.js 中我的路线是

app.post('/user/signup', async (req, res) => {
    await newUser.create(req.body)
    res.redirect('/');
})

My newUser.js file is我的 newUser.js 文件是

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const newUserSchema = new Schema({
    first_name: String,
    last_name: String,
    gmin: Number,
    email: String,
    password: String,
    type: String
});


const newUser = mongoose.model('newUser', newUserSchema);
module.expots = newUser;

First, I am not sure which mongoose.connect statement to use.首先,我不确定要使用哪个 mongoose.connect 语句。 One works but then I get the newUser.create is not a function and the other gives me an error before I can even start.一个有效,但后来我得到 newUser.create is not a function,另一个在我开始之前就给我一个错误。 So do I need the first mongoose.connect statement with the "useUnifiedTopology: true"?那么我是否需要第一个带有“useUnifiedTopology:true”的 mongoose.connect 语句? Or will I be ok with the second statement.或者我会接受第二个陈述。

Next does the order of the next four lines (app.set and the 3 app.use) matter how I order them.接下来的四行(app.set 和 3 app.use)的顺序与我如何排序有关。 Sometimes I get a throw error if I mess up the order.如果我弄乱了顺序,有时会出现抛出错误。 Does that order really matter?这个顺序真的重要吗?

Then the biggest issue is making the app.post route to work.然后最大的问题是让 app.post 路由工作。

Also when adding anything to a mongo database do the name attributes from the html form have to match the variables in the schema file?此外,在向 mongo 数据库添加任何内容时,html 表单中的名称属性是否必须与模式文件中的变量相匹配? So for example if I have "first_name" in the schema file but "fname" on the name attribute on the html form, will that cause an error?因此,例如,如果我在模式文件中有“first_name”,但在 html 表单的名称属性中有“fname”,这会导致错误吗?

Also in my mongodb I have an attribute called "type" to specify certain roles for the user.同样在我的 mongodb 中,我有一个名为“type”的属性来为用户指定某些角色。 Will the variable "type" give me issues down the road?变量“类型”会给我带来问题吗? Should I just change it now to "roles"?我现在应该把它改成“角色”吗?

Sorry for all the questions and thanks for the help抱歉所有问题,感谢您的帮助

Typo Detected!检测到错别字!

On your newUser.js在你的 newUser.js 上

module.expots = newUser;

you forgot a letter r there, it should be expo r ts instead of expots ;您在那里忘记了一个字母r ,它应该是 expo r ts 而不是expots

module.exports = newUser;

There is a typo in your code.您的代码中有错字。 Fix that module.exports修复那个 module.exports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM