gpt4 book ai didi

javascript - 无法在另一个 js 文件中加载自定义 Mongoose 架构

转载 作者:太空宇宙 更新时间:2023-11-04 02:04:55 24 4
gpt4 key购买 nike

我有这个项目文件夹结构:

Project folder structure -> see the image

我正在尝试在我的用户 ctrl 中导入我的用户架构,但是当我启动 nodemon users.ctrl.js 时,出现以下错误:

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

^

SyntaxError: Unexpected token

这是用户架构:

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
};

这是用户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
};

我哪里错了?有什么事情让我逃脱了吗?

最佳答案

默认情况下, Node 对应于存储 Node 包的/node_modules。因此,使用相对路径名来访问文件 require(./filename)

关于javascript - 无法在另一个 js 文件中加载自定义 Mongoose 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com