我有许多异步函数,我从文件中导入 json 然后设置字段。我等待导入 json。之前它曾经通过等待 json 然后操作数据来顺序执行。我以前使用过 Mocha,最近开始迁移到 Jest,那时我的所有测试和功能都开始崩溃。
我不确定我是否正确理解异步功能。但它以前工作得很好,现在不行了。下面是示例:
async build_transaction_request({reference=util.id_generator(), type="XYZ", amount=25, currency="USD", timestamp=util.timestamp_millis()}={}){
let transactions = await config_util.get_payload("transactions")
let x_transaction = transactions.xTransaction
x_transaction.reference = reference
x_transaction.type = type
x_transaction.amount = amount
x_transaction.currency = currency
x_transaction.timestamp = timestamp
return x_transaction
}
我收到此错误:
类型错误:无法设置未定义的属性“引用”
下面是另外两个函数:
async load_data(file_path) {
await delete require.cache[require.resolve(file_path)]
let data = await require(file_path);
return data
}
async get_payload(file_name) {
let payload = await this.load_data(this.root_dir + '/resources/payload/' + file_name + ".json")
return payload
}
下面是我的测试中调用 build_transaction_request 函数的代码:
test('Test xyz returns 201', async() => {
//......some code.....
var[instruction, x_transaction, token] = await Promise.all([
helper.build_instruction_request({
type: "CURRENCY",
value: value,
rate: rate
}),
helper.build_transaction_request({
amount: amount
}),
helper.build_token_request(token_id)
]);
});
问题是await config_util.get_payload("transactions")
一旦解决就返回一个对象,该对象要么缺少该xTransaction
属性,要么使用undefined
. 一旦您尝试关闭 的属性,代码就会爆炸undefined
,这就是您正在做的事情:x_transaction.reference = reference
。这就像在说:undefined.reference = reference
.... 因此,这就是代码繁荣的地方。你不能为内存中不存在的东西赋值。
您get_payload
只是根据您提供给load_data
.
你看你传入的参数了load_data
吗?
let payload = await this.load_data(this.root_dir + '/resources /payload/' + file_name + ".json")
你在那里有一个空间,所以它可能不正确。无论你在那里喂什么,它都找不到 json ......
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句