我是nodejs的新手,尝试通过浏览指南来制作API,但遇到了一个问题,即使使用google tho也无法解决。 这是我的命令行文字。
consign v0.1.2 Initialized in d:\Jinn\API
+ .\libs\config.js
+ .\db.js
+ .\libs\middlewares.js
+ .\routes\index.js
+ .\routes\tasks.js
+ .\libs\boot.js
d:\Jinn\API\db.js:22
sequelize = new _sequelize2.default(config.database, config.username, config
.password, config.params);
^
ReferenceError: sequelize is not defined
at Function.module.exports (d:/Jinn/API/db.js:10:5)
at Consign.into (d:\Jinn\API\node_modules\consign\lib\consign.js:239
:17)
at Object.<anonymous> (d:/Jinn/API/index.js:13:4)
at Module._compile (module.js:570:32)
at loader (d:\Jinn\API\node_modules\babel-register\lib\node.js:144:5
)
at Object.require.extensions.(anonymous function) [as .js] (d:\Jinn\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 d:\Jinn\API\node_modules\babel-cli\lib\_babel-node.js:161:27
at Object.<anonymous> (d:\Jinn\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! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\
node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.11.2
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! api@1.0.0 start: `babel-node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the 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 bitskinsapi 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 api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls api
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! d:\Jinn\API\npm-debug.log
似乎麻烦在db.js文件中的某处,所以这里是代码:
import fs from "fs";
import path from "path";
import Sequelize from "sequelize";
let db = null;
module.exports = app => {
if (!db) {
const config = app.libs.config;
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config.params
);
db = {
sequelize,
Sequelize,
models: {}
};
// Загрузка моделей
const dir = path.join(__dirname, "models");
fs.readdirSync(dir).forEach(file => {
const modelDir = path.join(dir, file);
const model = sequelize.import(modelDir);
db.models[model.name] = model;
});
Object.keys(db.models).forEach(key => {
db.models[key].associate(db.models);
});
}
return db;
};
说实话,我大部分都遵循说明和复制粘贴的代码片段,这就是为什么我感到沮丧,因为它应该有用。有什么想法吗?