我正在使用sequelize连接到mysql数据库进行开发。我有一个名为Dealer的模型:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Dealer = sequelize.define('Dealer', {
id: { allowNull: false, autoIncrement: true,
primaryKey: true, type: DataTypes.INTEGER.UNSIGNED },
...
created_at: { allowNull: false, type: DataTypes.DATE },
updated_at: { allowNull: false, type: DataTypes.DATE }
},
{underscored: true},
{
classMethods: {
associate: function(models) {
Dealer.hasMany(models.Job);
}
},
instanceMethods: {
getAllClientData: function(){
leads = [];
...
return leads;
},
}
});
return Dealer;
};
当我尝试在dealerController.js文件中的sequelize查询返回的对象上调用实例方法时:
dealer.getAllClientData()
我收到错误:
Unhandled rejection TypeError: dealer.getAllClientData is not a function
当我将返回的JSON打印到控制台时,它会这样读取:
{ dataValues:
{ id: 1,
....
}
...
'$modelOptions':
{ timestamps: true,
instanceMethods: {},
classMethods: {},
validate: {},
freezeTableName: false,
underscored: true,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: '1' },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
hooks: {},
indexes: [],
name: { plural: 'Dealers', singular: 'Dealer' },
omitNul: false,
...
}
...
}
显然我的instanceMethod没有定义,根据sequelize docs,我也应该有getter和setter。
我不明白我在这里缺少什么步骤,因为我已阅读了许多续集文档,甚至使用他们的cli来生成模型和迁移。
有什么想法吗?
以下是经销商的日志输出。原型
{ _customGetters: {},
_customSetters: {},
validators: {},
_hasCustomGetters: 0,
_hasCustomSetters: 0,
rawAttributes:
{ id:
{ allowNull: false,
autoIncrement: true,
primaryKey: true,
type: [Object],
Model: Dealer,
fieldName: 'id',
_modelAttribute: true,
field: 'id' },
///Other Attributes
},
_isAttribute: { [Function] cache: MapCache { __data__: [Object] } },
Model: Dealer,
$Model': Dealer }
答案 0 :(得分:0)
在进一步阅读文档并查看其他一些模型定义后,我发现问题是我错误地定义了我的模型。
在我上面的定义中,您会注意到我将2016-06-13 17:20:16,078 - ERROR - http_protocol - Traceback (most recent call last):
File "/opt/anaconda2/lib/python2.7/site-packages/daphne/http_protocol.py", line 108, in process
logger.debug("Upgraded connection %s to WebSocket %s", self.reply_channel, protocol.reply_channel)
AttributeError: 'WebSocketProtocol' object has no attribute 'reply_channel'
Unhandled Error
Traceback (most recent call last):
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/protocols/basic.py", line 571, in dataReceived
why = self.lineReceived(line)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/web/http.py", line 1692, in lineReceived
self.allContentReceived()
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/web/http.py", line 1781, in allContentReceived
req.requestReceived(command, path, version)
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/web/http.py", line 768, in requestReceived
self.process()
File "/opt/anaconda2/lib/python2.7/site-packages/daphne/http_protocol.py", line 143, in process
self.basic_error(500, b"Internal Server Error", "HTTP processing error")
File "/opt/anaconda2/lib/python2.7/site-packages/daphne/http_protocol.py", line 246, in basic_error
}).encode("utf8"),
File "/opt/anaconda2/lib/python2.7/site-packages/daphne/http_protocol.py", line 206, in serverResponse
http.Request.write(self, message['content'])
File "/opt/anaconda2/lib/python2.7/site-packages/twisted/web/http.py", line 951, in write
self.transport.writeSequence(l)
exceptions.AttributeError: 'NoneType' object has no attribute 'writeSequence'
选项括在括号中,然后将classMethods和instanceMethods包装在另一组括号中。
这是不正确的。定义续集模型的正确方法是使用两组括号,第一组包含模型属性,第二组包含所有其他选项,包括方法。
RewriteEngine On
DocumentRoot /var/www/html/django-ui
ProxyPass /static/ !
Alias /static/ /var/www/html/django-ui/cyberSA/static/
<Location "/">
ProxyPass "ws://localhost:8000/"
</Location>