I am importing a model in pages/api/submitAllegationApi.js and creating a new entry there. Strangely enough there's no error when I use the 'api/submitAllegationApi' alone. Once I call another api route, and then use 'api/submitAllegationApi' again, I get the error below:
warn - ./models/index.js Critical dependency: the request of a dependency is an expression error - Error: Cannot find module '/Users/Desktop/.../.next/server/pages/api/pdfcreate.js' at webpackEmptyContext (/Users/Desktop/.../.next/server/pages/api/submitAllegationApi.js:22:10) at eval (webpack-internal:///(api)/./models/index.js:18:65) at Array.forEach () at eval (webpack-internal:///(api)/./models/index.js:17:4) at Object.(api)/./models/index.js (/Users/.../.next/server/pages/api/submitAllegationApi.js:84:1) at webpack_require (/Users/.../.next/server/webpack-api-runtime.js:33:42) at eval (webpack-internal:///(api)/./pages/api/submitAllegationApi.js:6:24) at Object.(api)/./pages/api/submitAllegationApi.js (/Users/.../.next/server/pages/api/submitAllegationApi.js:95:1) at webpack_require (/Users/.../.next/server/webpack-api-runtime.js:33:42) at webpack_exec (/Users/.../.next/server/pages/api/submitAllegationApi.js:116:39) { code: 'MODULE_NOT_FOUND' }
Below is my submitAllegationApi:
const db = require('../../models/index');
const { DataTypes } = require("sequelize");
const Allegations=require("../../models/Allegations")(db.sequelize, db.Sequelize.DataTypes);
export default async function handler(req, res){
var data= req.body;
Allegations.create({
faculty: data.Faculty
...
})
console.log("received: "+data);
res.status(200).send(data);
};
Below is Allegation.js:
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Allegations extends Model {
static associate(models) {
// define association here
}
}
Allegations.init({
faculty: DataTypes.STRING
...
}, {
sequelize,
modelName: 'Allegations',
});
return Allegations;
};
And here's the index.js:
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;