更新到Sail.js 0.10.1后,我在尝试更新模型的特定实例的属性字段时遇到错误。我也试过做User.find(),然后设置user.attribute = someValue,然后调用user.save(),发生了同样的错误。
以下是相关代码:
// TODO: Save user params (for edit/updates)
save: function(req, res) {
var params = req.params.all(),
updateParams = {};
if (params['publicKey'] != '') {
sails.log.debug('Updating public key: ' + params['publicKey']);
updateParams['publicKey'] = params['publicKey'];
}
if (params['email'] != '') {
updateParams['email'] = params['email'];
}
sails.log.debug(updateParams);
User.update({ id: params['id'] }, updateParams, function (err) {
if (err) sails.log.error(err);
req.session.messages = { success: ['Successfully updated user profile'] };
if (req.session.user.admin) {
return res.redirect('/admin/manage_users');
} else {
return res.redirect('/');
}
});
},
我得到的错误是:
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: ER_BAD_FIELD_ERROR: Unknown column 'user.undefined' in 'order clause'
at Query.Sequence._packetToError (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Query.ErrorPacket (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:82:18)
at Protocol._parsePacket (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:213:24)
at Parser.write (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:75:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:110:48)
at PoolConnection.Connection.query (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:166:25)
at Query._callback (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/lib/adapter.js:1008:22)
at Query.Sequence.end (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
at Query._handleFinalResultPacket (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
at Protocol._parsePacket (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:213:24)
at Parser.write (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/home/kevin/workspace/test_moocRP/moocRP/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:75:28)
Details: Error: ER_BAD_FIELD_ERROR: Unknown column 'user.undefined' in 'order clause'
非常感谢一些指导。
编辑:我正在使用sails-mysql。 编辑:我打开登录本地MySQL数据库并监视正在执行的查询。以下是相关部分:140804 18:25:19 97 Query SELECT `user`.`id`, `user`.`firstName`, `user`.`lastName`, `user`.`email`, `user`.`registered`, `user`.`admin`, `user`.`publicKey`, `user`.`keyEmail`, `user`.`createdAt`, `user`.`updatedAt` FROM `user` AS `user` WHERE `user`.`id` = "991426" LIMIT 1
140804 18:25:41 98 Query SELECT `user`.`id`, `user`.`firstName`, `user`.`lastName`, `user`.`email`, `user`.`registered`, `user`.`admin`, `user`.`publicKey`, `user`.`keyEmail`, `user`.`createdAt`, `user`.`updatedAt` FROM `user` AS `user` WHERE `user`.`id` = "991426"
98 Query UPDATE `user` AS `user` SET `publicKey` = 'test2', `email` = '', `updatedAt` = '2014-08-04 18:25:41' WHERE `user`.`id` = "991426" ORDER BY `user`.`undefined` DESC
97 Query SELECT `user`.`id`, `user`.`firstName`, `user`.`lastName`, `user`.`email`, `user`.`registered`, `user`.`admin`, `user`.`publicKey`, `user`.`keyEmail`, `user`.`createdAt`, `user`.`updatedAt` FROM `user` AS `user`
我的用户模型定义为:
// Instance variables
id: {
type: 'STRING',
required: true,
unique: true
},
firstName: {
type: 'STRING',
required: true
},
lastName: {
type: 'STRING',
required: true
},
email: {
type: 'STRING',
email: true,
unique: true,
},
registered: {
type: 'BOOLEAN',
defaultsTo: false
},
admin: {
type: 'BOOLEAN',
defaultsTo: false
},
publicKey: {
type: 'TEXT',
required: true
},
keyEmail: {
type: 'TEXT',
email: true,
required: true
},
// Instance methods
toJSON: function() {
var obj = this.toObject();
delete obj._csrf;
return obj;
}
updateParams:{ publicKey: 'test2', email: '' }
params [&#39; id&#39;]:991426
答案 0 :(得分:6)
我认为这里的问题是你没有为模型设置主键。在primaryKey: true
字段上设置id
,错误就会消失。谢谢你的报告;如果您没有主键,我们可能会想要修补Waterline以发出警告。而且,你知道,不会崩溃:)
答案 1 :(得分:1)
我使用promises和postgres-sails进行了测试,但这对我有用:
User.update(1, {emailAddress: 'test@test.com'}).then(console.log);
唯一的主要区别是我在这里使用Q promises而不是回调。