mysql - Angular.js put 请求因 Sails.js 和 mysql 数据库而失败

标签 mysql angularjs sails.js waterline

在我的 Angular.js 服务中,我创建了一个自定义请求:

.factory('CalendarService', ['$resource',
          function($resource) {
              return {
                  Update: function(bookingObj) {
                      var BookingResource = $resource('http://localhost:1337/bookings/:id',{
                          id: '@id'
                      }, {
                          'update': {
                              method: 'PUT',
                              isArray: true
                          }
                      });
                      var bookingId = bookingObj.id;
                      return BookingResource.update({
                          id: bookingId
                      }, bookingObj).$promise;
                  }

在服务器端,Sails.js 似乎正确配置了 sails-mysql,我可以毫无错误地发出 get 请求,但是当我调用更新函数时,我收到此错误:

error: Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
: .findOne() requires a criteria. If you want the first record try  .find().limit(1)
    at module.exports.findOne (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\adapter\dql.js:143:56)
    at _runOperation (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\query\finders\operations.js:
400:29)
    at run (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\query\finders\operations.js:69:8)
    at bound.module.exports.findOne (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\query\finders
\basic.js:66:16)
    at bound [as findOne] (C:\prestigeweb\node_modules\sails\node_modules\lodash\dist\lodash.js:729:21)
    at Deferred.exec (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\query\deferred.js:501:16)
    at updated (C:\prestigeweb\node_modules\sails\lib\hooks\blueprints\actions\update.js:83:9)
    at bound (C:\prestigeweb\node_modules\sails\node_modules\lodash\dist\lodash.js:957:21)
    at applyInOriginalCtx (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\utils\normalize.js:416:80)
    at wrappedCallback (C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\utils\normalize.js:315:18)    
    at _normalizeCallback.callback.success (C:\prestigeweb\node_modules\sails\node_modules\waterline\node_modules\switchb
ack\lib\normalize.js:33:31)
    at _switch (C:\prestigeweb\node_modules\sails\node_modules\waterline\node_modules\switchback\lib\factory.js:48:28)
    at C:\prestigeweb\node_modules\sails\node_modules\waterline\lib\waterline\query\dql\update.js:224:9
    at done (C:\prestigeweb\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:135:19)
    at C:\prestigeweb\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:32:16
    at C:\prestigeweb\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:157:25 
Details:  Error: .findOne() requires a criteria. If you want the first record try .find().limit(1)

这是来自 Postman 的 HTTP 请求:

PUT /bookings/6397 HTTP/1.1
Host: localhost:1337
Accept: application/json, text/plain, */*
Origin: http://localhost:8100
X-DevTools-Emulate-Network-Conditions-Client-Id: ABAC3370-A7E6-42AA-A5DA-BA0205880B9C
X-FirePHP-Version: 0.0.6
User-Agent: Mozilla/5.0 (iPad; CPU OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5
Content-Type: application/json;charset=UTF-8
DNT: 1
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4,fr;q=0.2
Cache-Control: no-cache
Postman-Token: 0a627a0a-2f27-0e55-737f-5e838e1084e0

{"id":6397,bla bla bla}

这就是我配置 Sails.js 的方式:

1) sails 生成 api 预订

2) npm-install sails-mysql

3) 在 config/connections.js 中插入以下行

mysql: {
    adapter: 'sails-mysql',
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: '',
    database: 'prestige'
  },

4) 添加到 config/cors.js:

allRoutes: true,

methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD'

origin: '*'

5) 在 config/model.js 添加

module.exports.models = {

  connection: 'mysql',

   migrate: 'safe'

};

sails.js:模型:

module.exports = {

tableName: 'prenotazioni',
adapter: 'mysql',
//migrate: 'safe',
autoCreatedAt: false,
autoUpdatedAt: false,

attributes: {

  id: {
      type: 'integer'
  },
  numero_prenotazione: {
      type: 'string'
  },
  nome: {
      type: 'string'
  },
  cognome: {
      type: 'string'
  },
  civico: {
      type: 'string'
  },
  indirizzo: {
      type: 'string'
  },
  ditta: {
      type: 'string'
  },
  cap: {
      type: 'string'
  },
  citta: {
      type: 'string'
  },
  nazione: {
      type: 'string'
  },
  lingua: {
      type: 'string'
  },
  telefono: {
      type: 'string'
  },
  email: {
      type: 'string'
  },
  agente: {
      type: 'string'
  },
  numero_camere: {
      type: 'integer'
  },
  numero_ospiti: {
      type: 'integer'
  },
  costo_totale_camere: {
      type: 'integer'
  },
  ospite_1: {
      type: 'string'
  },

  ospite_2: {
      type: 'string'
  },
  camera_1: {
      type: 'string'
  },

  camera_2: {
      type: 'string'
  },
  numero_persone_1: {
      type: 'integer'
  },
  numero_persone_2: {
      type: 'integer'
  },
  data_prenotazione: {
      type: 'date'
  },
  data_arrivo_1: {
      type: 'date'
  },
  data_arrivo_2: {
      type: 'date'
  },
  data_partenza_1: {
      type: 'date'
  },
  data_partenza_2: {
      type: 'date'
  },
  quantita_1: {
      type: 'integer'
  },
  quantita_2: {
      type: 'integer'
  },
  totale_1: {
      type: 'integer'
  },
  totale_2: {
      type: 'integer'
  },
  prezzo_1: {
      type: 'integer'
  },
  prezzo_2: {
      type: 'integer'
  },
  status_1: {
      type: 'integer'
  },
  status_2: {
      type: 'integer'
  },
  duration: {
      type: 'string'
  }
 }
};

最佳答案

在你的模型中:

module.exports = {
    ...
    autoPK: false,

    attributes: {
        id: {
            type: 'integer',
            autoIncrement: true,
            primaryKey: true
        },
        ...
    }
}

进一步阅读:Waterline docs .

关于mysql - Angular.js put 请求因 Sails.js 和 mysql 数据库而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28287283/

相关文章:

mysql - 3个强实体集的数据库设计

node.js - 如何使用 .crt 文件在sails.js 中配置https

php - 多个表上的 SQL 查询

javascript - 如何使用 ssh2 和 node.js 计算远程服务器上的磁盘空间并存储值?

angularjs - Angular : Returning a $q. defer().promise 而不是 $http promise

angularjs - Angular ui-router 不在嵌套路由中调用子 Controller

node.js - Sentry :在带有raven-node的nodejs中setUserContext?

node.js - Sails 蓝图生命周期

mysql - 如何获取 MySQL 中空表的列的默认值?

php - 如何根据表中的列值连接表