我正在学习如何使用hapi服务器和其他一些软件包在Nodejs中设置基本mvc的教程。
教程:https://www.sitepoint.com/node-js-mvc-application/
Git for my project :https://github.com/christoph88/node-js-mvc-tut/
尝试启动服务器时出错:
~/Projects/node-js-mvc-tut$ node server.js
/home/christoph/Projects/node-js-mvc-tut/server.js:33
Models.sequelize.sync().then(() => {
^
TypeError: Cannot read property 'sync' of undefined
at Object.<anonymous> (/home/christoph/Projects/node-js-mvc-tut/server.js:33:17)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
模型在需求中定义。我不明白为什么同步是未定义的。 lib / index.js文件中需要Sequelise。
'use strict';
const Hapi = require('hapi');
const Hoek = require('hoek');
const Path = require('path');
const Settings = require('./settings');
const Routes = require('./lib/routes');
const Models = require('./lib/models/');
const server = new Hapi.Server();
server.connection({ port: Settings.port });
server.register([
require('vision'),
require('inert')
], (err) => {
Hoek.assert(!err, err);
server.views({
engines: { pug: require('pug') },
path: Path.join(__dirname, 'lib/views'),
compileOptions: {
pretty: false
},
isCached: Settings.env === 'production'
});
// Add routes
server.route(Routes);
});
Models.sequelize.sync().then(() => {
server.start((err) => {
Hoek.assert(!err, err);
console.log(`Server running at: ${server.info.uri}`);
});
});
有人可以帮我这个吗?能够让这个运行起来很棒,所以我可以尝试让它适应我的需要。
答案 0 :(得分:0)
我通过将index.js文件移动到models文件夹来实现它。
此文件具有必要的脚本,可以在模型中调度sequelise,从而解决问题。
答案 1 :(得分:0)
确保已将数据库导出到index.js中
module.exports = db
,并在文件开头声明了db变量
var db = {}