简体   繁体   English

无法在另一个js文件中加载自定义mongoose模式

[英]Fail to load custom mongoose schema in another js file

I've this project folder structure: 我有这个项目文件夹结构:

项目文件夹结构 - >查看图像

And I'm trying to import my user schema in my user ctrl but when i start nodemon users.ctrl.js gives me this error: 我正在尝试在我的用户ctrl中导入我的用户架构但是当我启动nodemon users.ctrl.js时给了我这个错误:

const {Users} = require('users.schema');

      ^

SyntaxError: Unexpected token 

This is the user schema: 这是用户架构:

const mongoose = require('mongoose');
const validator = require('validator');
// {
//   email: 'andrew@example.com',
//   password: 'adpsofijasdfmpoijwerew',
//   tokens: [{
//     access: 'auth',
//     token: 'poijasdpfoimasdpfjiweproijwer'
//   }]
// }
var Users = mongoose.model('Users', {
  email: {
    type: String,
    required: true,
    trim: true,
    minlength: 1,
    unique: true,
    validate: {
      validator: validator.isEmail,
      message: '{VALUE} is not a valid email'
    }
  },
  password: {
    type: String,
    required: true,
    minlength: 6
  },
  tokens: [{
    access: {
      type: String,
      required: true
    },
    token: {
      type: String,
      required: true
    }
  }]
});
module.exports = {Users}
And this is the user ctrl:

const mongoose = require('mongoose');
const {Users} = require('users.schema');

getAll = (req, res) => {
  // let id = req.params.userId;
  console.log('GET all cards');
  Users
    .find()
    .select('users')
    .exec((err, doc) => {
      console.log('Risultato: ', doc);
    });
};

module.exports = {
  getAll
};

And this is the user ctrl: 这是用户ctrl:

const mongoose = require('mongoose');
const {Users} = require('users.schema');

getAll = (req, res) => {
  // let id = req.params.userId;
  console.log('GET all cards');
  Users
    .find()
    .select('users')
    .exec((err, doc) => {
      console.log('Risultato: ', doc);
    });
};

module.exports = {
  getAll
};

Where am I wrong? 我哪里错了? Is there something that escapes me? 有什么东西逃脱了我吗?

By default node corresponds to /node_modules where the node packages are stored. 默认情况下,节点对应于存储节点包的/node_modules Hence use relative path names to access the file require(./filename) . 因此,使用相对路径名来访问文件require(./filename)

I solved the problem: 我解决了这个问题:

On this current pc that i'm using i had node version number 5.0.7 . 在我正在使用的当前电脑上我有节点版本号5.0.7。 To use the new ecma6 features i need to install v 8.0.... After installing the version I solved the problemAfter installing the version I solved the problem 要使用新的ecma6功能,我需要安装v 8.0 ....安装版本后我解决了问题安装完版本后我解决了问题

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

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