Cannot update attributes in Sails.js model instance

只愿长相守 提交于 2019-12-11 07:56:51

问题


After updating to Sail.js 0.10.1, I've been getting an error while trying to update attribute fields of a specific instance of a model. I've tried doing User.find() as well, then setting user.attribute = someValue, and then calling user.save(), and this very same error occurred.

Here is the relevant code:

// 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('/');
    }
  });
},

The error that I get back is:

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'

Some guidance would be much appreciated.

EDIT: I'm using sails-mysql.

EDIT: I turned on logging in my local MySQL database and monitored the query being executed. Here are the relevant lines:

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`

My user model is defined as:

// 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


回答1:


I think the issue here is that you don't have a primary key set for the model. Set primaryKey: true on the id field, and the error should go away. Thanks for the report; we'll probably want to patch Waterline to throw up a warning if you don't have primary key. And to, you know, not crash :)




回答2:


I tested this using promises and postgres-sails, however this worked for me:

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

The only major difference is that I'm using Q promises here instead of callbacks.

Source: Waterline Query Methods



来源:https://stackoverflow.com/questions/25129742/cannot-update-attributes-in-sails-js-model-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!