【问题标题】:Cannot read property 'sync' of undefined无法读取未定义的属性“同步”
【发布时间】:2017-11-08 16:15:54
【问题描述】:

我正在关注如何使用 hapi 服务器和其他一些包在 Nodejs 中设置基本 mvc 的教程。

教程https://www.sitepoint.com/node-js-mvc-application/

Git 用于我的项目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}`);
  });
});

有人可以帮我解决这个问题吗?让它运行会很棒,所以我可以尝试调整它以适应我的需求。

【问题讨论】:

    标签: javascript node.js model-view-controller sequelize.js


    【解决方案1】:

    确保您已在 index.js 中导出数据库

    module.exports = db
    

    并在文件开头声明了一个db变量

    var db = {}
    

    【讨论】:

      【解决方案2】:

      我已经通过将我的 index.js 文件移动到模型文件夹来让它工作。

      此文件具有在模型中调度 sequelise 从而解决问题的必要脚本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-03
        • 2015-10-10
        • 2020-04-06
        • 2020-10-30
        • 2019-10-09
        • 1970-01-01
        相关资源
        最近更新 更多