简体   繁体   English

如果不使用解构分配,则要求不工作

[英]require not working if not using destructuring assignment

In my nodejs , I have a small mongoose module which exports the model (User) . 在我的nodejs ,我有一个小的mongoose模块,可以exports model (User) When I require the module without using destructuring assignment and I create the new instance of the model using new operator, I get the error that the model is not a function. 当我要求不使用模块destructuring分配和使用我创建模型的新实例new运营商,我得到的error ,该模型是不是一个函数。 But if i use the destructuring assignment when I require the model, everything works fine. 但是,如果我在require模型时使用destructuring分配,则一切正常。 Not able to understand why. 不明白为什么。

User.js exports the model User.js导出模型

const mongoose = require('mongoose');

exports.User = mongoose.model('User', {
    email:{
        type: String,
        trim: true,
        minlength: 1,
        reuqired: true
    }
});

Below code throws error if I dont use destructuring operator on line 2 : 如果我在第2行不使用解构运算符,则以下代码将引发错误:
server.js server.js

const mongoose = require('../DB/mongoose');
const User = require('../Models/User');

console.log(typeof(User));

let user = new User({
    email: "sdfdsf"
});

server.js throws the below error: server.js引发以下错误:

let user = new User({
           ^

TypeError: User is not a constructor
    at Object.<anonymous> (F:\javascript\nodePractice\ToDoApp\server\server.js:6:12)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:576:3)

But If I use a destructuring assignment on line 2 , it works all fine. 但是,如果我在line 2使用destructuring分配,则一切正常。 server.js : server.js

const mongoose = require('../DB/mongoose');
const {User} = require('../Models/User');

console.log(typeof(User));

let user = new User({
    email: "sdfdsf"
});
const {User} = require('../Models/User');

is equivalent to 相当于

const User = require('../Models/User').User;
//                                    ^^^^^

The module object that require() returns (the exports object that your module filled) does have a .User property . require()返回的模块对象require()模块填充的exports对象)确实具有.User 属性 If you don't access that but try to use the module object as a constructor, it throws. 如果您不访问它,而是尝试将模块对​​象用作构造函数,则会抛出该异常。

To be explicit, you might want to use 明确地说,您可能要使用

const userModule = require('../Models/User');

console.log(typeof userModule);
console.log(typeof userModule.User);

let user = new userModule.User({
    email: "sdfdsf"
});

Alternatively, if you insist on doing const User = require('../Models/User'); 或者,如果您坚持使用const User = require('../Models/User'); , you can also make the constructor function the exported object by overwriting module.exports instead of creating a property on it: ,您还可以通过覆盖module.exports而不是在其上创建属性来使构造函数使用导出的对象:

const mongoose = require('mongoose');

module.exports = mongoose.model('User', {
    …
});

You are exporting an object that has a property called "User" that hosts the model. 您正在导出的对象具有托管模型的名为“用户”的属性。 When importing it you need to specify what part of the file you want to import, or you need to later specify what property you want to use. 导入时,您需要指定要导入文件的哪一部分,或者以后需要指定要使用的属性。 You can still use 您仍然可以使用

const User = require('../Models/User');

But you will need to call User.User , accessing exports.User later on: 但是,你需要调用User.User ,访问exports.User以后:

let user = new User.User({ //call exports.User from User model file
    email: "sdfdsf"
});

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

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