简体   繁体   English

我正在尝试在程序中使用猫鼬模式,但它一直显示错误用户不是构造函数

[英]I am trying to use mongoose Schema in my program but it keeps on showing error User is not a constructor

I am trying to use mongoose Schema in my program but it keeps on showing error User is not a constructor. 我正在尝试在程序中使用猫鼬模式,但它一直显示错误用户不是构造函数。

I'm not sure why exactly is this happening... 我不确定为什么会这样...

This is my current 'api.js' source code: 这是我当前的“ api.js”源代码:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
const Schema = mongoose.Schema;
const db = 'mongodb://localhost:27017/stfe';
mongoose.connect(db,(err) => {
    if(err) {
        console.log("error");
    }
    else {
        console.log("DB connected a port 27017");
    }
});
var User = '../models/userschema';

/* GET home page. */
router.get('/', function(req, res, next) {
    res.send("hello from the backend");
});

const newUser = {
    firstname:"Asim",
    lastname:"Dahal",
    email:"arsenalsim@gmail.com",   
    password:12321,
    cell_number:222,
    alternate_number:111
};

router.get('/insertuser', function(req, res, next) {

    var user = new User({
        firstname:"asim",
        lastname:"dahal",
        email:"arsenalasim@gmail.com",
        password:"12321", 
        cell_number:12321,
        alternate_number:12321
    });

    user.save((err) => {
        if(err) {
            console.log(err);
        }
        else {
            console.log('saved');
        }
    });
});

module.exports = router;

And this is my current userschema.js source code: 这是我当前的userschema.js源代码:

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

var UserSchema = new Schema({
  firstname: {
    type: String,
    required: true
  },
  lastname: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  cell_number: {
    type: Number,
    required: true
  },
  alternate_number: {
    type: Number,
    required: true
  }
})
// var UserSchema = mongoose.model('User', UserSchema);
module.exports = mongoose.model(User,UserSchema)

Error occurs while importing the User Model. 导入用户模型时发生错误。 In api.js, replace : 在api.js中,替换:

var User = '../models/userschema';

by 通过

var User = require('./models/userschema');

And in userschema.js, replace: 并在userschema.js中,替换为:

// var UserSchema = mongoose.model('User', UserSchema);
module.exports = mongoose.model(User,UserSchema)

by 通过

var User =mongoose.model('User', UserSchema);
module.exports = User;

Follow the above steps and you will get "saved" printed on the console when you hit the "insertuser" endpoint. 请按照上述步骤,当您单击“ insertuser”端点时,将在控制台上打印“保存”。

var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;

var UserSchema = new mongoose.Schema({

     //Blabla.....

});

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

And in your router 并在您的路由器中

const User = require('../model/user');

暂无
暂无

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

相关问题 Mongoose 错误:架构不是构造函数 - Mongoose Error: Schema is not constructor 我试图在注册/登录后在“创建配置文件”页面中更新我的用户架构,但数据未显示在 MongoDB 中 - I am trying to update my User schema in a "create profile" page after registering/logging in, but the data isn't showing up in MongoDB 我正在尝试在我的 react redux 应用程序中使用 map。 但使用useDispatch后,控制台显示错误 - I am trying to use map in my react redux app. but after using useDispatch, the console is showing error 我正在尝试在 JS 中使用可选链接,但它显示错误 - I am trying to use optional chaining in JS and it showing error 我的程序不断输出“未定义”。 我正在尝试创建一个 function,它使用一个调用 function 的按钮来更改我网页上的文本 - My program keeps outputting "undefined". I am trying to create a function that changes text on my webpage with a button that calls a function 我正在尝试使用mongoose设置我的mongoDB数据库,但是出现错误“ mongoose默认的Promise库已弃用” - I am trying to setup my mongoDB database using mongoose, but i getting an error “mongoose default promise library is deprecated” 我正在尝试返回 function 但显示错误 - I am trying to return function but showing error 我正在尝试在 javascript 中使用导入和导出,但它显示未捕获的错误。 这是代码: - I am trying to use import and export in javascript but it is showing uncaught error. Here is the code : 当我尝试在 django mvt 中使用 $.ajax 时,它无法正常工作,甚至没有显示错误 - When I am trying to use $.ajax in django mvt its not working not even showing error 尝试将对象导入猫鼬模式时出错 - getting error trying to import object into mongoose schema
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM