javascript - 单独文件中的 thinky 模型 : how to handle cyclical/circular dependencies

标签 javascript thinky

我尝试遵循示例 here ,但是运气不佳。

我在 user.js 文件中有用户模型:

import thinky from './thinky';
let type = thinky.type;
let User = thinky.createModel('User', {
  id: type.string(),
  username: type.string(),
});
export default User;

let Game = require('./game');
User.hasAndBelongsToMany(Game, "games", "id", "id");

game.js 文件中的游戏模型:

import thinky from './thinky';
let type = thinky.type;
let Game = thinky.createModel('Game', {
  id: type.string(),
  host: type.string()
});

export default Game;

let User = require('./user');
Game.hasAndBelongsToMany(User, "players", "id", "id");

当我尝试将它们导入 test.js 文件(在其中创建用户和游戏的实例)时,我得到hasAndBelongsToMany 的第一个参数必须是模型

我尝试不用es6语法来编写它,仍然不起作用......

最佳答案

我的示例是,如果您将导出默认值更改为 module.exports,一切都应该正常

关于javascript - 单独文件中的 thinky 模型 : how to handle cyclical/circular dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535343/

相关文章:

javascript - 将选择框事件中添加的动态表单元素依次对齐到选择框上方的页面顶部

javascript - 删除少量Keen IO事件"Too many events"错误

Javascript - 我如何为函数设置 'this' 变量

javascript - Thinky.io hasMany 不保存新模型实例

RethinkDB Thinky - 1 小时内的行

javascript - Promise.then 函数内部的外部变量没有被改变

javascript - Webpack:是否可以在编译时评估 javascript 表达式?

javascript - Getconnection() 不适用于状态机

javascript - Rethinkdb 和 Thinky 如何在具有 'hasMany' 关系的模型中的多个模型上 saveAll,其中父模型包含子模型数组