Home > Back-end >  I can't get my export to work for my Sequelize model
I can't get my export to work for my Sequelize model

Time:02-10

I'm creating an e-commerce site for a school project and I'm trying to connect my GoogleStrategy to my sqlite database so I can store the users in a table. But for some reason I get an error when calling the variable in my passport-setup file.

The error occurs when i try to call my exported User variable from user-model.js -> passport-setup.js

user-model.js:

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('./database.sqlite');

const User = sequelize.define('User', {
    googleId: {
        type:DataTypes.STRING,
        allowNull: false
    },
    displayName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    email: {
        type: DataTypes.STRING,
        allowNull: false
    },
    photo: {
        type: DataTypes.STRING,
        allowNull: false
    },
});

console.log(User === sequelize.models.User); //true

module.exports = User;

passport-setup.js:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require ('./keys');
const User = require('./user-model');

passport.use(
    new GoogleStrategy ({
        //options for the strategy
        callbackURL:'/redirect/',
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret,
    }, (accessToken, refreshToken, profile, done) => {
 
        console.log('passport callback function fired');
        console.log(profile)
    }) 
)

sequelize connection in index.js:

//initialize sequelize
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize({
    dialect: 'sqlite',
    storage: './database.sqlite'
  });

  
//check sequelize connection
  try {
    sequelize.authenticate();
    console.log('Connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }

Error:

[nodemon] starting `node index.js`
C:\Users\karwa\Studier\År 3\GIK2PG (Projektbaserad systemutveckling)\Sneakers-Project\node_modules\sequelize\dist\lib\sequelize.js:56
      options.dialect = urlParts.protocol.replace(/:$/, "");
                                          ^

TypeError: Cannot read property 'replace' of null
    at new Sequelize (C:\Users\karwa\Studier\År 3\GIK2PG (Projektbaserad systemutveckling)\Sneakers-Project\node_modules\sequelize\dist\lib\sequelize.js:56:43)
    at Object.<anonymous> (C:\Users\karwa\Studier\År 3\GIK2PG (Projektbaserad systemutveckling)\Sneakers-Project\passport-setup.js:24:19)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (C:\Users\karwa\Studier\År 3\GIK2PG (Projektbaserad systemutveckling)\Sneakers-Project\index.js:10:23)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
[nodemon] app crashed - waiting for file changes before starting...

I'm following this tutorial but i'm trying to implement sqlite3 and sequelize instead of mongodb and mongoose.

CodePudding user response:

You need to export sequelize instance in index.js and use it in user-model.js instead of creating one more Sequelize instance there:

const sequelize = require('./index');

const User = sequelize.define('User', {
...
  • Related