KeystoneJS模型中的Mongoose模式类型

时间:2014-04-08 15:55:28

标签: mongoose keystonejs

var keystone = require('keystone'),
    Types = keystone.Field.Types;

var LeaderboardEntry = new keystone.List('leaderboardEntry', {
  autokey: { path: 'slug', from: 'publicKey playername', unique: false }
});

LeaderboardEntry.add({
  publicKey: { type: String, required: true, noedit: true },
  playername: { type: String, required: true },
  playerid: { type: String },
  points: { type: Number, required: true },
  /*data: { type: Mixed, required: false, unique: false },*/
  publishedDate: { type: Types.Date }
});

字段data必须是Mixed类型,但遗憾的是没有匹配的KeystoneJS类型。

如何在keystone.List对象中完成此操作?

2 个答案:

答案 0 :(得分:13)

前段时间我遇到同样的问题并遇到了this Twitter post ..它不会显示在管理界面中,但可以通过这种方式添加一个Mongoose字段类型。

在上面的代码之后添加:

LeaderboardEntry.schema.add({ data: mongoose.Schema.Types.Mixed });

答案 1 :(得分:0)

您可以在模型文件中定义

var keystone = require('keystone');
var mongoose = require('mongoose'); // "npm i --save mongoose" of course
var Types = keystone.Field.Types;

var FBEntry = new keystone.List('FBEntry');

FBEntry.add({
    type: { type: String, index: true },
    // data: mongoose.Schema.Types.Mixed    <--- define overhere will throw exception
});

FBEntry.schema.add({ data: mongoose.Schema.Types.Mixed }); // you should define outside .add()

FBEntry.defaultColumns = 'type, data';
FBEntry.register();