【问题标题】:Not Seeing Id In Sails JS Response在 Sails JS 响应中看不到 ID
【发布时间】:2013-07-12 04:29:45
【问题描述】:

因此,我正在使用 SailsJS 来尝试让 API 快速启动并运行。我还没有设置数据存储(可能会使用 mongodb),但我看到我假设的东西就像 SQLite 数据库或其他东西。所以我为用户生成了一个模型和控制器。所以在浏览器中我点击了用户/创建。我看到 createdAt 和 updatedAt 但没有 Id。我是否需要真实的数据存储才能查看 ID?这仍然是您免费获得的东西吗?

适配器

// Configure installed adapters
// If you define an attribute in your model definition, 
// it will override anything from this global config.
module.exports.adapters = {

    // If you leave the adapter config unspecified 
    // in a model definition, 'default' will be used.
    'default': 'disk',

    // In-memory adapter for DEVELOPMENT ONLY
    // (data is NOT preserved when the server shuts down)
    memory: {
        module: 'sails-dirty',
        inMemory: true
    },

    // Persistent adapter for DEVELOPMENT ONLY
    // (data IS preserved when the server shuts down)
    // PLEASE NOTE: disk adapter not compatible with node v0.10.0 currently 
    //              because of limitations in node-dirty
    //              See https://github.com/felixge/node-dirty/issues/34
    disk: {
        module: 'sails-dirty',
        filePath: './.tmp/dirty.db',
        inMemory: false
    },

    // MySQL is the world's most popular relational database.
    // Learn more: http://en.wikipedia.org/wiki/MySQL
    mysql: {
        module      : 'sails-mysql',
        host        : 'YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS',
        user        : 'YOUR_MYSQL_USER',
        password    : 'YOUR_MYSQL_PASSWORD',
        database    : 'YOUR_MYSQL_DB'
    }
};

型号

/*---------------------
    :: User
    -> model
---------------------*/
module.exports = {

    attributes: {
        firstName: 'STRING',
        lastName: 'STRING'
    }

};

【问题讨论】:

  • 尝试将此添加到您的模型中。 autoPk: true,

标签: sails.js


【解决方案1】:

.../user/findAll 列出所有用户。每个用户都应该有一个“id”属性,例如

{
   "username":"admin",
   "password":"$2a$10$SP/hWtlJINkMgkAuAc9bxO1iWsvVcglwEU4ctGCiYpx.7ykaFWt56",
   "createdAt":"2013-07-12T17:36:29.852Z",
   "updatedAt":"2013-07-12T17:36:29.852Z",
   "id":1
}

【讨论】:

  • 没看到。以下是我所看到的。 [{"firstName":"Chad","createdAt":"2013-07-12T04:13:16.917Z","updatedAt":"2013-07-12T04:13:16.917Z"},{"firstName":"Chad","lastName":"Hietala","createdAt":"2013-07-12T04:13:33.538Z","updatedAt":"2013-07-12T04:13:33.538Z"},{"createdAt":"2013-07-12T04:15:44.243Z","updatedAt":"2013-07-12T04:15:44.243Z"},{"createdAt":"2013-07-12T04:15:47.704Z","updatedAt":"2013-07-12T04:15:47.704Z"},{"createdAt":"2013-07-12T04:16:15.506Z","updatedAt":"2013-07-12T04:16:15.506Z"}]
  • /config/adapters.js 配置使用的数据库。我得到一个带有'default:"disk"'和'default:"memory"'的'id'属性。你能发布你的 /config/adapters.js 和 /api/models.User.js 吗?
  • 另外:.../user/find/1 是否向您显示了第一个用户?多试几次。
  • 用文件更新了我的帖子。当我尝试使用 id 查找特定用户时,我收到了 Not Found 消息。
  • 我有相同的 adapter.js 和 user.js 很好。我正在使用最新版本的sails 0.8.94(出现在升降机的船下)。您可能使用的是 0.9 开发版吗?
【解决方案2】:

他们删除了 findAll 你可以简单地使用 find instad

你也可以这样请求http://example.de/Model/create?firstname=Foo&lastname=bar

【讨论】:

    【解决方案3】:

    我通过在同一个 Sails 应用程序中的不同数据库之间切换来检查这一点,并且它工作得非常好。

    因此,在这种情况下,您可以将 autoPK 的属性添加到模型中并将其设置为 true,如下所示:

    module.exports = {
        attribute:{
           //your model attributes with validations.
        },
        autoPK:true
    }
    

    如果这对您不起作用,那么我认为您在系统上安装的sails 副本有问题。

    请尝试使用 npm 安装较新版本 (v0.9.8) 的 Sails,或尝试使用较新版本 (v0.9.8) 详细信息更新您的 package.json 并执行 npm install。

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 2019-03-14
      • 2018-12-17
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2023-03-06
      • 2015-12-04
      • 2016-05-13
      相关资源
      最近更新 更多