简体   繁体   English

NodeJS MongoDB 架构问题,“ReferenceError:未定义字符串”

[英]NodeJS MongoDB Schema Issue, "ReferenceError: string is not defined"

I am new to nodeJS and am trying to connect a front end form to a mongoDB atlas database.我是 nodeJS 的新手,正在尝试将前端表单连接到 mongoDB 地图集数据库。 I have an console error "ReferenceError: string is not defined" at my file named "userModel.js".我在名为“userModel.js”的文件中出现控制台错误“ReferenceError:未定义字符串”。

I have provided other linked files in the projects, although I am not sure if it's necessary to do so.我在项目中提供了其他链接文件,但我不确定是否有必要这样做。

userModel.js用户模型.js

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

const userSchema = new Schema({
    email: {
        type: string,
        required: true
    },
    password: {
        type: string,
        required: true
    },
    confirmpassword: {
        type: string,
        required: true
    }
  }, {timestamps: true})

  const User = mongoose.model('User', userSchema);
  module.exports = User;

signup.ejs注册.ejs

<form action="/signupRoutes.js" method="post">
     <label for="email">Email:</label>
     <input type="text" id="email" name="email"><br><br>
     <label for="password">Password:</label>
     <input type="text" id="password" name="password"><br><br>
     <input type="submit" value="Submit">
 </form>

server.js服务器.js

const express = require('express');
const app = express();
const mongoose = require('mongoose');
const ejs = require('ejs');

const homepageRoutes = require('./routes/homepageRoutes');
const signupRoutes = require('./routes/signupRoutes');
const loginRoutes = require('./routes/loginRoutes');

const dotenv = require('dotenv');
dotenv.config();
const dbusername= process.env.DB_USERNAME;
const dbpassword= process.env.DB_PASSWORD;
dbURI = `mongodb+srv://${dbusername}:${dbpassword}@testingcluster.ix9mpsg.mongodb.net/?retryWrites=true&w=majority`;
mongoose.connect(dbURI).then((result) => app.listen(4000)).catch((err) => console.log(err));

app.set('view engine', 'ejs');
app.use(express.static('assets'));

app.use("/index", homepageRoutes);
app.use("/signup", signupRoutes);
app.use("/login", loginRoutes);

signupRoutes.js注册路由.js

const express = require('express');
const router = express.Router();

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

router.get('/', (req, res) => {
    res.render('./signup');
    res.end();
});

app.post('/signup', (req, res) => {
    const user = new User({
        email: req.body.email,
        password: req.body.password
    }); 
    user.save();
    res.redirect('/');
});

module.exports = router;

Thanks for your time:)谢谢你的时间:)

Sidenote: I am aware that this is not a secure way to store passwords, I am just testing posting to mongoDB for now.旁注:我知道这不是存储密码的安全方式,我现在只是测试发布到 mongoDB。

You have three types in your User Schema (email, password, confirmpassword).您的用户架构中有三种类型(电子邮件、密码、确认密码)。 You only provide two in the '/signup' route.您只在“/注册”路线中提供两个。 Delete the confirmpassword type in the User Schema.删除用户架构中的确认密码类型。 You don't need to save the password two times just check if its the same.您不需要保存两次密码,只需检查它是否相同。

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

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