使用Sails.js和mysql数据库时,Angular.js put请求失败

时间:2015-02-02 21:41:07

标签: 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)

这是邮递员的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)帆生成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:model:

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'
  }
 }
};

1 个答案:

答案 0 :(得分:2)

在你的模特中:

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

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

进一步阅读:Waterline docs