Sequelize does not recognize the findOne property

I've been working on creating a model using Sequelize with a MySQL database. When trying to post to '/students/register', I keep encountering an error stating that findOne is not a function. I attempted requiring mysql without success and also tried using a different function like findAll, which also didn't work. Can anyone help identify what the issue might be? Thanks

models/Students.js

const Sequelize = require('sequelize');
const sequelize = require('../database/db')

module.exports = (sequelize, Sequelize) => {
    const Student = sequelize.define(
        'student', {
            id: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            name: {
                type: Sequelize.STRING
            },
            email: {
                type: Sequelize.STRING
            },
            password: {
                type: Sequelize.STRING
            },
            created: {
                type: Sequelize.DATE,
                defaultValue: Sequelize.NOW
            }
        }, {
            timestamps: false
        });
    module.exports = Student;
};

database/db.js

   const Sequelize = require('sequelize')
const sequelize = new Sequelize('canavs', 'root', 'root', {
    host: 'localhost',
    port: 8889,
    dialect: 'mysql',
    operatorAliases: false,

    pool: {
        max: 5,
        min: 0,
        acquire: 3000,
        idle: 10000
    }
})
// sequelize.import('./models/Students')
module.exports = sequelize;

index.js

const Student_Info = require('./models/Students')
const db = require('./database/db')



// app.use('/students', Student)

app.get('/getName', (req, res) => {
    Student_Info.findOne({
            where: {
                id: 1
            }
        })
        .then((student) => {
            res.json(student.name);
        })
        .catch((err) => {
                res.send('error' + err)
            }

        )
})

Answer №1

The student model definition is missing a return statement.

module.exports = (sequelize, Sequelize) => {
    const Student = sequelize.define(
        'student', {
            ...
        }, {
            timestamps: false
        });
    return Student; // Make sure to include the return statement for the Student model in its definition
};

I trust this explanation clears things up...

Answer №2

Let's simplify things by only exporting the student model without the need to export Sequelize or sequelize(db). Instead of exporting require it, you can simply require it when needed!

const Sequelize = require('sequelize');
const sequelize = require('../database/db');

const Student = connection.define('student', {

    name: Sequelize.STRING,
    email: {
        type: Sequelize.STRING,
        validate: {
            isEmail: true
        }
    },
    password: {
        type: Sequelize.STRING,
        validate: {
            isAlpha: true
        }
    }
});


module.exports = Student;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js/Express.js is throwing an error: 'undefined is not a function'

I recently started using 'express-namespace' to organize my routing in a cleaner way. Below is the snippet of code I have implemented. .. 9 var controllers = require('./controllers'); 10 require('express-namespace'); .. 46 ...

Having trouble fetching data (node.js) from my client (react.js)

I'm experiencing difficulty retrieving the data I send from the client to the server. Below is my react code: axios.post('http://localhost:5000/post-entry', { data: formData }) .then(res => { console.log(res); }) This is my server cod ...

npm - Error: EPERM: unable to perform operation, unlink operation restricted

My system details are: Windows 10 1703 Node 6.11.2 npm 5.4.0 Encountering an error while trying to install an npm package (e.g. npm i gulp-notify): npm ERR! path C:\Users\web-dev\Desktop\barber\node_modules\fsev ...

The free version of Neo4j and Linkurious for the community

Is it possible to integrate linkurious.js community edition with Neo4j? I have heard about the sigma-parser-cypher plugin. As a beginner in both Neo4j and Linkurious, your patience is appreciated. Can both Linkurious JS and Neo4j be run on the same machi ...

Why is the localhost not loading despite having the node server and mongodb running?

I recently began learning about MEAN development. I managed to set up my express server and establish a connection with mongodb. When I run node server in the terminal, the server starts running and the connection to mongo is successful. However, when I tr ...

Dockerizing Microservices - A New Approach to Architecture

I am currently developing a micro-services project that utilizes docker technology. Within this project, I have a specific micro-service tasked with listening and retrieving data from multiple sources. My goal is to enable the capability of dynamically s ...

The file or directory does not exist: .. ode_modules.staging@types eact-transition-group-96871f11package.json

I'm facing an issue while attempting to run a React project in VS2013. I followed a tutorial from here and initially, everything seemed to be working fine. However, when I tried to customize the project by adding more packages to the package.json fil ...

Utilizing Cypress with Electron: A Guide to File System Operations

I have been utilizing Cypress to run tests on my Electron application. Due to Cypress operating in browser mode, the FS module is not compatible. As a result, I am encountering this error: Error in mounted hook: "TypeError: fs.existsSync is not a func ...

Update to the newest Node version on Windows using npm

I'm encountering an issue while trying to update Node.js to the latest version on my machine. When I run the following commands through npm, I receive the error below: npm install -g n This is the error I encountered: npm ERR! code EBADPLATFORM npm ...

Can both a Node image and a Python image be run simultaneously within a single Dockerfile?

I am considering putting my logic and backend in python and the frontend in React/Typescript. However, I am unsure if it's feasible to have a single Dockerfile that incorporates both a python image and a node image. Should I pursue this approach or o ...

What level of performance can be expected from Chokidar in Node.js?

One of the server’s features is a caching engine that stores all accessed files within a main directory. I am considering implementing Chokidar to monitor the entire directory tree (including subdirectories) for any changes in files, and automatically ...

What could be causing the createReadStream function to send a corrupted file?

My current task involves generating a file from a URL using the fs module to my local system. Initially, everything seems successful. However, when attempting to post this file into a group using the createReadStream() function, I encounter an issue where ...

Can someone guide me on incorporating values from an external server into the app.post function?

After successfully completing the registration process for my app, where users can register and log in to a shop, I encountered a hurdle with the login functionality. Let me walk you through the issue. The function below checks my mongoDB collection to se ...

Leverage your current ExpressJS application to function as a Firebase application

I currently have an app running on Heroku. This web application is quite simple, without any background jobs or a database. It consists of three endpoints - one to serve HTML content, another for handling POST requests to the backend, and a third endpoint ...

Guide on using webpack to import jQuery

When using webpack, is it necessary to install the jquery file from npm, or can I simply require the downloaded file from the jquery website? directory app/ ./assets/javascripts/article/create/base.js ./assets/javascripts/lib/jquery-1.11.1.min.js webpack ...

Adding information to two tables in PHP

I have been searching around this forum, and while I have found similar questions that have already been answered, I am still struggling to figure this out (as always, I'm a complete newbie and need clear explanations). In my database, I have tables ...

What is the process for exporting/importing a variable in Node.js?

What is the correct way to export/import a variable in Node.js? I attempted to use export and import methods, but I received an error message stating that it should be a module. After changing the type to module in the JSON file, it then told me that requ ...

The NodeJS expressJs Server is unable to receive JSON data

Here is how my ajax request is structured: $.ajax({ url: baseURL, type: 'POST', data: JSON.stringify(sendData), contentType: 'application/json; charset=utf-8', dataType: 'json', async: false, succe ...

Why are imported modules unable to reach global variables in Node?

As I delve deeper into using ES6 and organizing my code across multiple files for better readability and version control, I've encountered an issue that has me puzzled. In one of my scenarios, I have a class defined in a separate file called class.js, ...

Issue with PHP registration system not functioning properly on phpmyadmin and wampserver

I'm having trouble getting my php signup system to connect to my locally hosted phpmyadmin database. I've double-checked for spelling errors and everything seems correct, but the header won't change as it should in the PHP sign up script. Ad ...