无法更新Sails.js模型实例中的属性

在更新到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数据库日志logging,并监视正在执行的查询。 这里是相关的行:

 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 ['id']: 991426

Solutions Collecting From Web of "无法更新Sails.js模型实例中的属性"

我认为这里的问题是你没有为模型设置主键。 在id字段上设置primaryKey: true ,并且错误应该消失。 感谢报告; 如果你没有主键,我们可能会打补丁来发出警告。 而且,你知道,不会崩溃:)

我用promises和postgres-sailstesting了这个,但是这对我有用:

 User.update(1, {emailAddress: 'test@test.com'}).then(console.log); 

唯一的区别是我在这里使用了Q的承诺,而不是callback。

来源:水线查询方法