2

我尝试按照此处的示例进行操作,但是没有运气。

我在 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 文件时,我在其中创建用户和游戏的实例,我得到First argument of hasAndBelongsToMany must be a Model

我试图在没有 es6 语法的情况下编写它,仍然无法正常工作......

4

3 回答 3

2

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

于 2015-12-11T05:15:20.977 回答
1

我们需要避免循环引用所以..

用户.js

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

游戏.js

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

export default Game;

index.js

import User from './user';
import Game from './game';

Game.hasAndBelongsToMany(User, "players", "id", "id");
User.hasAndBelongsToMany(Game, "games", "id", "id");

export {User, Game};
于 2015-11-18T14:07:29.957 回答
0

你也可以试试这个加载器,它的设计目的是加载多个模型定义并让它们对你的应用程序可用。

https://github.com/mwielbut/thinky-loader

于 2016-05-09T15:51:27.497 回答