简体   繁体   English

尝试在sails.js上更新模型时出错

[英]Get error when trying to .update() a model on sails.js

As you'll see in the code below I'm able to find the user because I get the console.log('log1', userSettings.PASSWORD); 正如您将在下面的代码中看到的那样,我能够找到用户,因为我得到了console.log('log1',userSettings.PASSWORD); printed in the terminal, and I get a user.Settings.PASSWORD returned. 打印在终端上,我得到一个user.Settings.PASSWORD返回。 So up to this point everything works fine. 因此,到目前为止,一切正常。 But when I attempt to do the update I end up getting the error: 但是,当我尝试进行更新时,最终会收到错误消息:

I get the following error: Details: RequestError: Invalid column name 'id' 我收到以下错误:详细信息:RequestError:无效的列名称'id'

This is how my code looks like: 这是我的代码的样子:

  updateSettings: function(params) {
    var Promise = require('bluebird');
    return new Promise(function(fullfill, reject) {
      sails.models.customer.findOne()
        .where({
          USERNAME: params.username,
          PASSWORD: params.currentPassword
        }).exec(function(err, userSettings) {
          if (err) {
            reject(new Error('Error finding user'));
            console.error(err);
          }else if (userSettings) {
            console.log('log1', userSettings.PASSWORD);
            sails.models.customer.update({
              USERNAME: params.username
            },
            {
              PASSWORD: params.newPassword
            }).exec(function(err, userSettingsUpdated) {
              if (err) {
                console.error(err);
                reject(new Error('Error updating the user settings'));
              }else {
                fullfill(userSettingsUpdated);

              }
            });
          }

        });
    });
  }

Sails.js / Waterline already implements Bluebird as the promise library, so you can use the query methods as promises (depending on the version you use, of course). Sails.js / Waterline 已经将Bluebird实现为Promise库,因此您可以将查询方法用作Promise(当然,取决于所使用的版本)。

I took the liberty to rewrite your code. 我冒昧地重写了您的代码。 I transformed sails.models.customer into Customer . 我将sails.models.customer转换为Customer Excuse this please if there is a reason you are using sails.models.customer . 如果您有使用原因sails.models.customer

As galactocalypse already stated, the error may not be in the code you posted though. 正如galactocalypse已经指出的那样,该错误可能不在您发布的代码中。 I assume the error might occur because you don't have an ID attribute in your Customer model that sails wants to use to save your Customer instance. 我假设可能会发生错误,因为您的Customer模型中没有可航行用于保存Customer实例的ID属性。

Just post back if you need more help. 如果您需要更多帮助,请发回邮件。

updateSettings: function(params) {

    var where = {
        USERNAME: params.username,
        PASSWORD: params.currentPassword
    };

    return Customer.findOne().where(where)
        .then(function(userSettings) {
            sails.log.info('log1', userSettings.PASSWORD);
            return Customer.update({ USERNAME: params.username }, {PASSWORD: params.newPassword});
        })
        .then(function(userSettingsUpdated) { /* do whatever you want here */ })
        .catch(function(err) { sails.log.error(err); });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM