Home>

I'm currently making a system and I'm creating a model part, but I can't run it with an error

Is there something wrong with the description?

First time to practice MVC in Node.js, how to write method? I would like to know if you can create

var sqlite3 = requre ('sqlite3');
var db = new sqlite3.Datadase ('ChieproDB.sqlite3');
var addMember (men_name, mem_age, mem_join, mem_skill) {
    db.run ('insert into member (mem_name, mem_age, mem_join, mem_skill) values ​​(?,?,?,?)', men_name, mem_age, mem_join, mem_skill);
}
module.exports = {addMember}


  • Answer # 1

    MVC is not very relevant, or it's just a syntax error.
    If you want to create a function,

    function addMember () {
      // processing
    }


    If you want to behave as a function of an object (= this case)

    var addMember = function () {
      // processing
    }

    is better.

    If the environment can use grammar after ES2015

    var addMember = () =>{
      // processing
    }
    You can also write

    .
    It is called an arrow function.

    function ()and() =>are strictly different in movement (the binding of this). Please look.

    If it is ES2015 or later, there is a story that it is better to declare withletconstinstead ofvar, but it is not the main subject, so it is omitted To do.

  • Answer # 2

    var sqlite3 = requre ('sqlite3');
    var db = new sqlite3.Datadase ('ChieproDB.sqlite3');
    var addMember = (men_name, mem_age, mem_join, mem_skill) =>{
        db.run ('insert into member (mem_name, mem_age, mem_join, mem_skill) values ​​(?,?,?,?)', men_name, mem_age, mem_join, mem_skill);
    }
    module.exports = {addMember}
    Solved by writing