我有這個項目的文件夾結構:無法加載自定義模式貓鼬在另一個JS文件
而且我試圖導入我的用戶CTRL我的用戶模式,但是當我開始nodemon用戶.ctrl.js給了我這個錯誤:
const {Users} = require('users.schema');
^
SyntaxError: Unexpected token
這是用戶模式:
const mongoose = require('mongoose');
const validator = require('validator');
// {
// email: '[email protected]',
// 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
};
這是用戶對話框:
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
};
我在哪裏錯了?有什麼能逃脫我嗎?
使用相對路徑。節點試圖從'node_modules'解析給定的包名稱。簡單地用require('./ users.schema')替換require語句。 –