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: This is the user schema: And this is the user ctrl: Where am I wrong? Is the"/>
  简体   繁体   中英

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:

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:

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. Hence use relative path names to access the file require(./filename) .

I solved the problem:

On this current pc that i'm using i had node version number 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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