0

架构如下所示:

import Game from './Game'
import {io, thinky} from '../'

const type = thinky.type


export const playerSchema = {
  id: type.string(),
  createdAt: type.date().default(thinky.r.now()),
  modifiedAt: type.date(),
  gameId: type.string(),
  name: type.string().default('Anon'),
  socket: type.string(),
  disconnected: type.boolean().default(false),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default([])
    }).default([])
  })).default([])
}

当我尝试保存级别具有undefined值的玩家时,我得到:
“无法读取属性'回合' undefined

进一步说明:有时一个玩家会在其他玩家完成第一关后进入游戏。所以,我希望数组中这个索引的值保持不变undefined

示例数据(在 YAML 中):
id: 1337
...
levels:
-
- rounds:
  - ...
    tries:
    - answer: [2, 4]
      ...

如果我更改playerSchema.levelstype.array(),我会得到:
“数组 [levels](位置 0)中的元素不能是undefined

评论后编辑:

即使使用默认值,这也不起作用......

var thinky = require('thinky')({
  db: 'slam'
})
var type = thinky.type
var r = thinky.r


var playerSchema = {
  id: type.string(),
  createdAt: type.date().default(thinky.r.now()),
  modifiedAt: type.date(),
  gameId: type.string(),
  name: type.string().default('Anon'),
  socket: type.string(),
  disconnected: type.boolean().default(false),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema(type.object().schema({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema(type.object().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default({})).default([])
    }).default({})).default([])
  }).default({})).default([])
}

var Player = thinky.createModel('Player', playerSchema)

var player = new Player({
  levels: [undefined, {}]
})

player.save().then(console.log)

结果是 ...

/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/schema.js:92
      field = field[path[j]];
                   ^

TypeError: Cannot read property 'rounds' of undefined
    at generateDefault (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/schema.js:92:20)
    at Object.generateDefault (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/schema.js:86:11)
    at model.Document._generateDefault (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/document.js:172:16)
    at new model (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/model.js:131:11)
    at Object.<anonymous> (/Users/arnar/git/slam-web-app/app/tests/test-player-schema.js:36:14)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

我什至尝试创建一个pre validation hook,但这没有任何效果......

Player.pre('validate', function(next) {
  this.levels = this.levels.map(function(l) {
    return l != null ? l : {}
  })
  next()
})
4

1 回答 1

0

编辑跟进您的评论。这就是您想要的(您基本上需要在数组模式的定义中使用 default({}):

var thinky = require('./lib/thinky.js')();
var type = thinky.type;
var r = thinky.r;


var Player = thinky.createModel('Player', {
  id: type.string(),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema(type.object({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default({})
    }).default({})).default([])
  })).default([])
})

var player = new Player({
  levels: [{}] 
});

player.save().then(console.log);

thinky的作者在这里。这对我有用:

var thinky = require('./lib/thinky.js')();
var type = thinky.type;
var r = thinky.r;


var Player = thinky.createModel('Player', {
  id: type.string(),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default([])
    }).default({})
  })).default([])
})

var player = new Player({
  levels: undefined
});

player.save().then(console.log);

在旁注中,您可能需要替换一个default([])default({})因为您正在设置类型对象。

于 2016-01-02T09:29:25.970 回答