如何解决“使用`sails-mongo`时,主键必须具有`columnName:'_id'`'

时间:2019-05-16 06:11:11

标签: sails.js waterline sails-mongo

我正在使用:

  • “风帆”:“ 1.2.1”,
  • “ sails-mongo”:“ 1.0.1”

当我获得mongodb集合的文档时,出现如下错误:

In model `archive`: 
debug: The default primary key attribute (`id`) is not set up correctly.
debug: When using `sails-mongo`, primary keys MUST have `columnName: '_id'`,
debug: and must _not_ have `autoIncrement: true`.
debug: Also, in most cases (unless `dontUseObjectIds` has been set to `true` for the model),
debug: then the `type` of the primary key must also be `string`.

sails.config.datastores.js

 module.exports.datastores = {
  default: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    database: 'test',
  }
};

sails.config.models.js

module.exports.models = {

  migrate: 'safe',
  attributes: {
    createdAt: { type: 'number', autoCreatedAt: true, },
    updatedAt: { type: 'number', autoUpdatedAt: true, },
    id: { type: 'string', columnName: '_id' },
  },
  dataEncryptionKeys: {
    default: 'RWcFGJN8+at5E7eIwNCIQxkR7P0nRAW8Fg4c9tzwFTw='
  },
  cascadeOnDestroy: true
};

api.models.User.js

module.exports = {

  tableName: 'user',
  attributes: {
    name: {
      type: 'string'
    },
    age: {
      type: 'number',

    }
  },

};

运行 api.controllers.UserController.js

时出现错误
module.exports = {
  getUsers: function (req, res) {
    let users = User.find();
    return res.send(users);
  },
};

0 个答案:

没有答案