架构如下所示:
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()
,我会得到:
“数组中的元素[级别](位置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()
})
答案 0 :(得分:0)
修改后续评论。 这就是你想要的(你基本上需要一个默认的({})在数组模式的定义中:
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);
思想的作者在这里。这对我有用:
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({})
,因为您正在设置类型对象。