架构如下所示:
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
。
id: 1337
...
levels:
-
- rounds:
- ...
tries:
- answer: [2, 4]
...
如果我更改playerSchema.levels
为type.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()
})