我正在尝试将工厂模型与非持久模型相关联,但出现错误:
TypeError: model.get is not a function
at SequelizeAdapter.get (node_modules/src/adapters/DefaultAdapter.js:13:18)
at AssocAttrs.get [as getAttribute] (node_modules/src/generators/Generator.js:15:46)
at AssocAttrs.getAttribute (node_modules/src/generators/AssocAttrs.js:7:23)
at tryCatch (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /Users/daniel.pan/Documents/projects/BudgetApp/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
这是我拥有的工厂:
import models from '../../models'
import faker from 'faker/locale/en'
export default factory.define('Expense', models.Expense, {
id: factory.seq('id'),
value: faker.finance.amount(0, 200),
note: faker.lorem.sentence(),
categoryId: factory.assocAttrs('Category', 'id'),
userId: factory.assocAttrs('User', 'id')
})
我正在尝试将“费用”与“类别”和“用户”相关联。 我可以只使用“ assoc”,它可以工作,但这是我不想要的持久化模型。文档说要使用“ assocAttrs”来嵌入无法持久保存的模型,但是我总是会收到类型错误。我不知道该如何进行。
答案 0 :(得分:0)
这太晚了,但是请从以下位置更改categoryId
的代码行:
categoryId: factory.assocAttrs('Category', 'id'),
收件人:
categoryId: factory.assocAttrs('Category'),
在调用时应导致JSON输出
factory.build('Expense').then(x => console.log(x)).
您会注意到,您的categoryId
属性将包含一个JSON对象,该对象将包含初始化为您定义它们的属性。那就是Expense.category = {propA: 'whatever you've defined', probB: 'etc...', categoryId: '123-213-123'}
为确保在上述JSON对象中将Expense.categoryId
属性仅设置为categoryId
,您需要将我上面提到的代码行更改为:
categoryId: factory.assocAttrs('Category')().then(y => y.categoryId)