首页 > 解决方案 > Mongo Atlas 中没有创建集合

问题描述

This is my .js file that contains the routes .


var express = require("express");
    var bcrypt = require("bcryptjs");
    var router = express.Router();
    var passport = require("passport");

    //User model
    var User = require("../models/StudentUser.js");
    router.post("/studentRegister",(req,res) =>
    {
        const {username,password,phone_no,student_id,dob,school,batch}=req.body;
        let errors = [];
        if(!student_id|| !username || !password || !phone_no || !dob || !school || !batch ){
        errors.push({msg:"Please fill in all fields."});
        }
        if(errors.length>0)
        {
            res.render("../views/signupStudent.ejs",{
                errors,
                username,
                password,
                phone_no,
                student_id,
                dob,
                school,
                batch
            });
        }
        else{
            var newUser = new User({
                username,
                password,
                phone_no,
                student_id,
                dob,
                school,
                batch
            });
            newUser.save(function(err)
   {
       if(err){
           console.log(err);
       }
       else
       {
        res.redirect("../views/dashboard.ejs");
       console.log("success");
       }
   });
        }


    });

    module.exports = router;

以下是我创建的架构:

var mongoose = require("mongoose");
var passportLocalMongoose= require("passport-local-mongoose");

var StudentUserSchema = new mongoose.Schema({
    username:{
        type: String,
        required: true

    },
    password:{
        type: String,
        required: true

    },
    student_id:{
        type:String,
        required:true
    },
    phone_no:{
        type: String,
        required: true

    },
    dob:{
        type: Date,
        default: Date.now

    },
    school:{
        type: String,
        required: true
    },
    batch:{
        type: String,
        required: true
    }
});
StudentUserSchema.plugin(passportLocalMongoose);
module.exports = User =  mongoose.model("User",StudentUserSchema);

我在文件中使用了 console.log(newUser) 来检查新用户详细信息是否已更新,但没有显示任何内容。我的 Atlas Cluster 中也没有创建任何集合。MongoDB 连接已确认,但没有进一步的进展。错误处理也不起作用,也不会引发错误。

请暂时忽略我的代码的错误处理方面。我主要关心的是确保正在创建与我的集群中的模式相对应的集合(在确认连接时我已正确包含的关键 api)。谢谢你!

标签: node.jsmongodbmongoosebackend

解决方案


推荐阅读