How to fix "TypeError: Crud.Select_products is not a function”, error in NodeJS












0















I'm trying to access an exported class from the Crud.js file, but I'm getting this error. My goal is to execute a sql query.



Error:



TypeError: Crud.Select_products is not a function
at C:xampphtdocsreactcrudserver.js:7:24
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:281:22
at Function.process_params (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:335:12)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:275:10)
at expressInit (C:xampphtdocsreactcrudnode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)


I'm using NodeJs in version 10.15.0, I already tried changing the way to export, but I still have the same error.



Server.js



var express = require('express');
var Crud = require('./database/Crud');
var app = express();


app.get('/api/home', function(req, res){
let results = Crud.Select_products();
res.send(results);

});

app.listen(5000, () => console.log('Escutando na porta 5000'));


Crud.js



var Connect = require('./Connect');

Class Crud {

constructor(){
Connect.Connecting();
}

Select_products(where){
if(where !== undefined){
let sql = 'Select * from produtos where id_produto = ?';
Connect.query(sql, [where], function(err, result){
if(err) throw err;
return result;
});
}else{
Connect.query('Select * from produtos', function(err, result){
if(err) throw err;
return result;
});
}
}
}

module.exports = Crud;


I expect to receive the data from database.










share|improve this question

























  • I don't know what connect does, but I cannot see how Select_products would be an element of Crud right now. You have to put it into the class, right now it is just a local function in the Crud file

    – user3637541
    Jan 2 at 22:40











  • The file Connect just connect to database.

    – Showtime
    Jan 2 at 22:43











  • Select_products is inside the class, it's just not formatted correctly - looks like it lost some tabs when it was pasted in, but that should work fine.

    – Joseph
    Jan 2 at 22:44













  • Oh, didn't see that, you are right @Joseph. Did you try to instanciate Crud? meaning using new Crud()?

    – user3637541
    Jan 2 at 22:53
















0















I'm trying to access an exported class from the Crud.js file, but I'm getting this error. My goal is to execute a sql query.



Error:



TypeError: Crud.Select_products is not a function
at C:xampphtdocsreactcrudserver.js:7:24
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:281:22
at Function.process_params (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:335:12)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:275:10)
at expressInit (C:xampphtdocsreactcrudnode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)


I'm using NodeJs in version 10.15.0, I already tried changing the way to export, but I still have the same error.



Server.js



var express = require('express');
var Crud = require('./database/Crud');
var app = express();


app.get('/api/home', function(req, res){
let results = Crud.Select_products();
res.send(results);

});

app.listen(5000, () => console.log('Escutando na porta 5000'));


Crud.js



var Connect = require('./Connect');

Class Crud {

constructor(){
Connect.Connecting();
}

Select_products(where){
if(where !== undefined){
let sql = 'Select * from produtos where id_produto = ?';
Connect.query(sql, [where], function(err, result){
if(err) throw err;
return result;
});
}else{
Connect.query('Select * from produtos', function(err, result){
if(err) throw err;
return result;
});
}
}
}

module.exports = Crud;


I expect to receive the data from database.










share|improve this question

























  • I don't know what connect does, but I cannot see how Select_products would be an element of Crud right now. You have to put it into the class, right now it is just a local function in the Crud file

    – user3637541
    Jan 2 at 22:40











  • The file Connect just connect to database.

    – Showtime
    Jan 2 at 22:43











  • Select_products is inside the class, it's just not formatted correctly - looks like it lost some tabs when it was pasted in, but that should work fine.

    – Joseph
    Jan 2 at 22:44













  • Oh, didn't see that, you are right @Joseph. Did you try to instanciate Crud? meaning using new Crud()?

    – user3637541
    Jan 2 at 22:53














0












0








0








I'm trying to access an exported class from the Crud.js file, but I'm getting this error. My goal is to execute a sql query.



Error:



TypeError: Crud.Select_products is not a function
at C:xampphtdocsreactcrudserver.js:7:24
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:281:22
at Function.process_params (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:335:12)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:275:10)
at expressInit (C:xampphtdocsreactcrudnode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)


I'm using NodeJs in version 10.15.0, I already tried changing the way to export, but I still have the same error.



Server.js



var express = require('express');
var Crud = require('./database/Crud');
var app = express();


app.get('/api/home', function(req, res){
let results = Crud.Select_products();
res.send(results);

});

app.listen(5000, () => console.log('Escutando na porta 5000'));


Crud.js



var Connect = require('./Connect');

Class Crud {

constructor(){
Connect.Connecting();
}

Select_products(where){
if(where !== undefined){
let sql = 'Select * from produtos where id_produto = ?';
Connect.query(sql, [where], function(err, result){
if(err) throw err;
return result;
});
}else{
Connect.query('Select * from produtos', function(err, result){
if(err) throw err;
return result;
});
}
}
}

module.exports = Crud;


I expect to receive the data from database.










share|improve this question
















I'm trying to access an exported class from the Crud.js file, but I'm getting this error. My goal is to execute a sql query.



Error:



TypeError: Crud.Select_products is not a function
at C:xampphtdocsreactcrudserver.js:7:24
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:xampphtdocsreactcrudnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)
at C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:281:22
at Function.process_params (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:335:12)
at next (C:xampphtdocsreactcrudnode_modulesexpresslibrouterindex.js:275:10)
at expressInit (C:xampphtdocsreactcrudnode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (C:xampphtdocsreactcrudnode_modulesexpresslibrouterlayer.js:95:5)


I'm using NodeJs in version 10.15.0, I already tried changing the way to export, but I still have the same error.



Server.js



var express = require('express');
var Crud = require('./database/Crud');
var app = express();


app.get('/api/home', function(req, res){
let results = Crud.Select_products();
res.send(results);

});

app.listen(5000, () => console.log('Escutando na porta 5000'));


Crud.js



var Connect = require('./Connect');

Class Crud {

constructor(){
Connect.Connecting();
}

Select_products(where){
if(where !== undefined){
let sql = 'Select * from produtos where id_produto = ?';
Connect.query(sql, [where], function(err, result){
if(err) throw err;
return result;
});
}else{
Connect.query('Select * from produtos', function(err, result){
if(err) throw err;
return result;
});
}
}
}

module.exports = Crud;


I expect to receive the data from database.







javascript node.js express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 1:12









iagowp

1,40411326




1,40411326










asked Jan 2 at 22:37









ShowtimeShowtime

153




153













  • I don't know what connect does, but I cannot see how Select_products would be an element of Crud right now. You have to put it into the class, right now it is just a local function in the Crud file

    – user3637541
    Jan 2 at 22:40











  • The file Connect just connect to database.

    – Showtime
    Jan 2 at 22:43











  • Select_products is inside the class, it's just not formatted correctly - looks like it lost some tabs when it was pasted in, but that should work fine.

    – Joseph
    Jan 2 at 22:44













  • Oh, didn't see that, you are right @Joseph. Did you try to instanciate Crud? meaning using new Crud()?

    – user3637541
    Jan 2 at 22:53



















  • I don't know what connect does, but I cannot see how Select_products would be an element of Crud right now. You have to put it into the class, right now it is just a local function in the Crud file

    – user3637541
    Jan 2 at 22:40











  • The file Connect just connect to database.

    – Showtime
    Jan 2 at 22:43











  • Select_products is inside the class, it's just not formatted correctly - looks like it lost some tabs when it was pasted in, but that should work fine.

    – Joseph
    Jan 2 at 22:44













  • Oh, didn't see that, you are right @Joseph. Did you try to instanciate Crud? meaning using new Crud()?

    – user3637541
    Jan 2 at 22:53

















I don't know what connect does, but I cannot see how Select_products would be an element of Crud right now. You have to put it into the class, right now it is just a local function in the Crud file

– user3637541
Jan 2 at 22:40





I don't know what connect does, but I cannot see how Select_products would be an element of Crud right now. You have to put it into the class, right now it is just a local function in the Crud file

– user3637541
Jan 2 at 22:40













The file Connect just connect to database.

– Showtime
Jan 2 at 22:43





The file Connect just connect to database.

– Showtime
Jan 2 at 22:43













Select_products is inside the class, it's just not formatted correctly - looks like it lost some tabs when it was pasted in, but that should work fine.

– Joseph
Jan 2 at 22:44







Select_products is inside the class, it's just not formatted correctly - looks like it lost some tabs when it was pasted in, but that should work fine.

– Joseph
Jan 2 at 22:44















Oh, didn't see that, you are right @Joseph. Did you try to instanciate Crud? meaning using new Crud()?

– user3637541
Jan 2 at 22:53





Oh, didn't see that, you are right @Joseph. Did you try to instanciate Crud? meaning using new Crud()?

– user3637541
Jan 2 at 22:53












1 Answer
1






active

oldest

votes


















0














Crud is a class, and you are trying to access a method, methods belongs to instances.



You can either export a instance, creating a singleton pattern like:



module.exports = new Crud();


Or you can create an instance at your server.js, and call the method



var Crud = require('./database/Crud');
var crud = new Crud();

app.get('/api/home', function(req, res){
let results = crud.Select_products();
res.send(results);
});


Also, when you get this to work, you will realize you have more problems. Your Select_products() function doesn't return anything. You might want to send a callback function to it, or use async/await and promises, if the library you are using supports promises






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014084%2fhow-to-fix-typeerror-crud-select-products-is-not-a-function-error-in-nodejs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Crud is a class, and you are trying to access a method, methods belongs to instances.



    You can either export a instance, creating a singleton pattern like:



    module.exports = new Crud();


    Or you can create an instance at your server.js, and call the method



    var Crud = require('./database/Crud');
    var crud = new Crud();

    app.get('/api/home', function(req, res){
    let results = crud.Select_products();
    res.send(results);
    });


    Also, when you get this to work, you will realize you have more problems. Your Select_products() function doesn't return anything. You might want to send a callback function to it, or use async/await and promises, if the library you are using supports promises






    share|improve this answer






























      0














      Crud is a class, and you are trying to access a method, methods belongs to instances.



      You can either export a instance, creating a singleton pattern like:



      module.exports = new Crud();


      Or you can create an instance at your server.js, and call the method



      var Crud = require('./database/Crud');
      var crud = new Crud();

      app.get('/api/home', function(req, res){
      let results = crud.Select_products();
      res.send(results);
      });


      Also, when you get this to work, you will realize you have more problems. Your Select_products() function doesn't return anything. You might want to send a callback function to it, or use async/await and promises, if the library you are using supports promises






      share|improve this answer




























        0












        0








        0







        Crud is a class, and you are trying to access a method, methods belongs to instances.



        You can either export a instance, creating a singleton pattern like:



        module.exports = new Crud();


        Or you can create an instance at your server.js, and call the method



        var Crud = require('./database/Crud');
        var crud = new Crud();

        app.get('/api/home', function(req, res){
        let results = crud.Select_products();
        res.send(results);
        });


        Also, when you get this to work, you will realize you have more problems. Your Select_products() function doesn't return anything. You might want to send a callback function to it, or use async/await and promises, if the library you are using supports promises






        share|improve this answer















        Crud is a class, and you are trying to access a method, methods belongs to instances.



        You can either export a instance, creating a singleton pattern like:



        module.exports = new Crud();


        Or you can create an instance at your server.js, and call the method



        var Crud = require('./database/Crud');
        var crud = new Crud();

        app.get('/api/home', function(req, res){
        let results = crud.Select_products();
        res.send(results);
        });


        Also, when you get this to work, you will realize you have more problems. Your Select_products() function doesn't return anything. You might want to send a callback function to it, or use async/await and promises, if the library you are using supports promises







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 23:00

























        answered Jan 2 at 22:51









        iagowpiagowp

        1,40411326




        1,40411326
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014084%2fhow-to-fix-typeerror-crud-select-products-is-not-a-function-error-in-nodejs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter