[API-REST-NODEJS] Problema com setup do Sequelize

apis-rest-nodejs
node-express
node.js

#1

Tive problemas com as configurações do capítulo 5.2 do livro "Construindo APIs Rest com Node.js"
Após finalizar as configurações do capítulo tentei rodar o npm start e apresenta o erro no terminal:

**npm start**

> ntask-api@1.0.0 start /home/developer/Documentos/NodeApi/ntask-api
> babel-node index.js

consign v0.1.2 Initialized in /home/developer/Documentos/NodeApi/ntask-api
+ ./db.js
+ ./models/tasks.js
+ ./libs/middlewares.js
+ ./routes/index.js
+ ./routes/tasks.js
+ ./libs/boot.js
/home/developer/Documentos/NodeApi/ntask-api/libs/boot.js:4
app.db.sync().done(function () {
      ^

TypeError: Cannot read property 'sync' of undefined
at Function.module.exports (/home/developer/Documentos/NodeApi/ntask-api/libs/boot.js:2:5)
at Consign.into (/home/developer/Documentos/NodeApi/ntask-api/node_modules/consign/lib/consign.js:239:17)
at Object.<anonymous> (/home/developer/Documentos/NodeApi/ntask-api/index.js:12:6)
at Module._compile (module.js:570:32)
at loader (/home/developer/Documentos/NodeApi/ntask-api/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/developer/Documentos/NodeApi/ntask-api/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Function.Module.runMain (module.js:604:10)
at /home/developer/Documentos/NodeApi/ntask-api/node_modules/babel-cli/lib/_babel-node.js:161:27
at Object.<anonymous> (/home/developer/Documentos/NodeApi/ntask-api/node_modules/babel-cli/lib/_babel-node.js:162:7)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)

npm ERR! Linux 4.10.0-37-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "start"
npm ERR! node v6.11.4
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! ntask-api@1.0.0 start: `babel-node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ntask-api@1.0.0 start script 'babel-node index.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ntask-api package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     babel-node index.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs ntask-api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls ntask-api
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/developer/Documentos/NodeApi/ntask-api/npm-debug.log

Arquivo index.js

import express from "express";
import consign from "consign";

const app = express();

consign()
    .include("db.js")
    .then("models")
    .then("libs/middlewares.js")
    .then("routes")
    .then("libs/boot.js")
    .into(app);

Arquivo db.js

import Sequelize from "sequelize";

const config = require("./libs/config.js");
let sequelize = null;

module.exports = () => {
    if (!sequelize) {
        sequelize = new Sequelize(
            config.database,
            config.username,
            config.password,
            config.params
        );
    }
}

Arquivo boot.js

module.exports = app => {
    app.db.sync().done(() => {
        app.listen(app.get("port"), () => {
            console.log(`NTASK API - porta ${app.get("port")}`);
        });
    });
}

Arquivo config.js

module.exports = {
    database: "ntask",
    username: "",
    password: "",
    params: {
        dialect: "sqlite",
        storage: "ntask.sqlite",
        define: {
            underscored: true
        }
    }
};

Antes de realizar essas configurações não tinha tido problemas.
Uso sistema operacional Ubuntu.

Agradeço qualquer ajuda! :smiley:


#2

Descobri o erro, faltava um return sequelize no arquivo db.js

import Sequelize from "sequelize";

const config = require("./libs/config.js");
let sequelize = null;

module.exports = () => {
    if (!sequelize) {
        sequelize = new Sequelize(
            config.database,
            config.username,
            config.password,
            config.params
        );
    }
    return sequelize;
}