gpt4 book ai didi

sequelize.js - aexmachina/factory-girl(节点)无法将工厂与非持久模型关联起来

转载 作者:行者123 更新时间:2023-12-03 22:37:45 26 4
gpt4 key购买 nike

我试图将工厂模型与非持久模型相关联,但出现错误:

     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”来嵌入未持久化的模型,但是我总是遇到类型错误。我不知道如何继续。

最佳答案

这已经很晚了,但是将 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'}
为确保 Expense.categoryId 属性仅设置为上述 JSON 对象中的 categoryId,您需要将我上面提到的代码行更改为:
categoryId: factory.assocAttrs('Category')().then(y => y.categoryId)

关于sequelize.js - aexmachina/factory-girl(节点)无法将工厂与非持久模型关联起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56351029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com