1

我在尝试使用 nodeJs 配置 postrgres 时遇到问题,我按照本教程进行操作,但我收到一个错误,该错误没有指定任何关于错误的内容:

这是堆栈跟踪

Unhandled rejection Error: Include unexpected. Element has to be either a Model, an Association or an object.
    at C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\lib\model.js:1976:15
    at Array.map (native)
    at conformOptions (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\lib\model.js:1941:39)
    at Model.<anonymous> (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\lib\model.js:700:7)
    at Model.tryCatcher (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\util.js:26:23)
    at Promise._settlePromiseFromHandler (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:503:31)
    at Promise._settlePromiseAt (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:577:18)
    at Promise._settlePromiseAtPostResolution (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\promise.js:244:10)
    at Async._drainQueue (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\async.js:128:12)
    at Async._drainQueues (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\async.js:133:10)
    at Immediate.Async.drainQueues (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\node_modules\bluebird\js\main\async.js:15:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

我的模型:

var express = require('express');
var models = require('../models');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
  var User = models.User;
  User.findById(1).then(function (user) {
    console.log(user);
  })
});

module.exports = router;

我的路由器:

“使用严格”;

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    username: DataTypes.STRING,
    id: DataTypes.INTEGER,
    email: DataTypes.STRING
  });

  return User;
};
4

2 回答 2

0

models导出带有参数的函数(sequelize, DataTypes),但在调用时没有带上提供的参数。您首先需要使用正确的参数调用模型函数,然后将返回的模型分配给变量。最丑陋的情况,你可以写:

var User = models(sequelize, DataTypes).User;

...在初始化sequelizeDataTypes在您的路由器中之后。但是您可能应该在模型文件中执行此操作并将该文件重新构造为具有初始化程序。

于 2021-09-23T04:17:42.780 回答
0

有可能您的模型路径的深度有问题,请先检查这是否是正确的路径。或者,您可以按照此处的建议管理您的 require 导入,这将非常方便和有用。

于 2017-05-12T13:09:24.077 回答