sails.js - sails v1.0 : error while using custom primary key with mongo

标签 sails.js sails-mongo

我正在尝试 SailsJS (v1.0.0-32) 的测试版,但在配置自定义 ID 时遇到了一些问题。波纹管你会发现我当前的配置:
modelExample.js

module.exports = {

  attributes: {
    id:{
      type: 'string',
      columnName: '_id'
    },
    attr: {
      type: 'number'
    }
  }
}

模型配置 config/models.js
attributes: {
    createdAt: { type: 'number', autoCreatedAt: true, },
    updatedAt: { type: 'number', autoUpdatedAt: true, },
    id: { type: 'string', columnName: '_id' },
}

试图插入的元素:
{id:"600000", attr:40}error我在尝试创建包含在尝试创建的元素中的属性“id”的记录时得到:
AdapterError: Unexpected error from database adapter: Invalid primary key value provided for `id`.  Cannot interpret `600000` as a Mongo id.
(Usually, this is the result of a bug in application logic.)

似乎 mongo 不喜欢字符串 600000作为 id ,但我不确定我是否误解了与 mongo 中的 id 相关的内容。在旧版本的风帆中,我从来没有遇到过这个问题,因为 id 覆盖很简单。

有关更多信息,sails-mongo 适配器版本为:"sails-mongo": "^1.0.0-5"

最佳答案

为了在 sails-mongo 中使用非 ObjectID 主键在 Sails 1.0 中,您必须设置 dontUseObjectIds: true在您的模型中,例如:

// api/models/User.js
module.exports = {
  dontUseObjectIds: true,
  attributes: {
    id: { type: 'number', columnName: '_id' }, // <-- still need to set `columnName`!
    name: { type: 'string' },
    ...etc...
  }
}

这是从 sails-mongo 开始实现的v1.0.0-7。

关于sails.js - sails v1.0 : error while using custom primary key with mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003762/

相关文章:

javascript - 请求正文中的 Facebook Messenger API 内容不完整

sails.js - 模型属性类型 : what is the difference between json and ref?

json - Elasticsearch:如何在将 json 数据上传到 Elasticsearch 之前创建映射

javascript - jquery post 方法不起作用(403 - 禁止)

node.js - 如何使 Sails 模型随时随地指向不同的数据库?

node.js - 在 SailsJS 中维护模型更新历史

node.js - Sails.js 模型 : create 2 association to self failed

node.js - sailsjs 更改为 mongodb 后仍然使用默认数据库

node.js - 未在 sails.js afterDisconnect() 中显示存储在 session 中的变量

javascript - 按天分组水线结果