Mongoose schema types in KeystoneJS models

人盡茶涼 提交于 2019-12-10 17:19:13

问题


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 }
});

The field data needs to be of type Mixed but unfortunately there is not a matching KeystoneJS type.

Any ideas how this can be done within a keystone.List object?


回答1:


A while back I ran into the same issue and ran across this Twitter post .. It won't show up in the admin UI, but it's possible to add a Mongoose field type this way.

Add this after your code above:

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



回答2:


You can define like this in your model file

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();


来源:https://stackoverflow.com/questions/22942132/mongoose-schema-types-in-keystonejs-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!