Reputation: 8164
My User.js
const mongoose = require('mongoose');
const joigoose = require("joigoose")(mongoose);
const { Joi } = require('celebrate');
var joiUserSchema = Joi.object({
body: Joi.object().keys({
username: Joi.string().required(),
name: Joi.string().required(),
email: Joi.string().email().required(),
role: Joi.string().default('admin')
}),
query: {
token: Joi.string().token().required()
}
});
var mongooseUserSchema = new mongoose.Schema(joigoose.convert(joiUserSchema));
User = mongoose.model('User', mongooseUserSchema);
module.exports = mongoose.model('User', mongooseUserSchema);
module.exports = joiUserSchema;
In my index.js
const {User, joiUserSchema} = require('./User');
console.log(User);
console.log(joiUserSchema);
Both are undefined
in terminal.
If I try REPL
> const {User} = require('./User');
Uncaught TypeError: Assignment to constant variable.
at Object.<anonymous> (/home/milenko/pract/joi1/User.js:19:6)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
How to debug this? I want to extend User validation model.
Upvotes: 1
Views: 113
Reputation: 1074008
The second one of these lines overwrites the effect of the first:
module.exports = mongoose.model('User', mongooseUserSchema);
module.exports = joiUserSchema;
You're trying to make both of them the one export from the module.
Your import is trying to use properties of an exported object. To match the import, the export should export an object with those named properties:
module.exports = {
User: mongoose.model('User', mongooseUserSchema),
joiUserSchema
};
Upvotes: 1