Как исправить 'При использовании `sails-mongo` первичные ключи ДОЛЖНЫ иметь` columnName:' _id'` ' - PullRequest
1 голос
/ 16 мая 2019

Я использую:

  • "паруса": "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);
  },
};
...