[英]Type script array push doesn't do anything
I'd like to lead off with, this isn't the async issues that other people have posted about where their pushes aren't doing anything.首先,我想说的是,这不是其他人发布的关于他们的推送没有做任何事情的异步问题。 Here is my code
这是我的代码
function createHolderPosition(holder: Holder, position: Position): void {
if(holder.positions == null){
holder.positions = []
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const length = holder.positions!.push(position.id);
log.info("new length {}", [length.toString()])
log.info("POsition id to add {}", [position.id])
log.info("POsition {}", [holder.positions!.toString()])
log.info("POsition {}", [holder.positions![0]])
And here is the output of these log statements这是这些日志语句的 output
💬 new length 1
💬 POsition id to add 123
💬 POsition
🛠 Mapping aborted at ~lib/array.ts, line 106, column 42, with message: Index out of range
So.所以。 The push statement returns 1, indicating that the array is now length 1 (as you'd expect).
push 语句返回 1,表示数组现在的长度为 1(如您所料)。 The position.id that we pushed definitely wasn't null. But when we try to print the array, we get nothing?
我们推送的 position.id 肯定不是 null。但是当我们尝试打印数组时,我们什么也得不到? And we get an index out of range error when we try to access the element we just thought we pushed
当我们尝试访问我们认为我们推送的元素时,我们得到一个索引超出范围的错误
--EDIT I think I figured it out Here are the the definitions of Holder and Position, in my graphql schema. --EDIT 我想我明白了这是我的 graphql 架构中 Holder 和 Position 的定义。
type Position @entity {
# Wallet address - Position (0x0000-xStella)
id: ID!
# xStella, STELLA-GLMR, STELLA-USDC, Vaults, TriPool
type: String!
# Amount of Stella
amount: BigInt!
# Time
timestamp: BigInt!
holder: Holder!
}
type Holder @entity {
# Wallet address
id: ID!
# Holding
stellaHolding: BigInt!
# Timestamp
lastTransaction: BigInt!
positions: [Position!] @derivedFrom(field: "holder")
}
And here is my generated code这是我生成的代码
get positions(): Array<string> | null {
let value = this.get("positions");
if (!value || value.kind == ValueKind.NULL) {
return null;
} else {
return value.toStringArray();
}
}
So, even though I'm new to typescript, what this looks like to me is that this getPositions function is returning a new instance of the array (contrary to what you might intuitively expect).因此,尽管我是 typescript 的新手,但在我看来,这个 getPositions function 正在返回数组的一个新实例(与您的直觉预期相反)。 I was able to actually push to the array by doing the following
我能够通过执行以下操作实际推送到数组
function createHolderPosition(holder: Holder, position: Position): void {
if(holder.positions == null){
holder.positions = []
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const newPositions: string[] = holder.positions!
newPositions.push(position.id)
holder.positions = newPositions
If positions
is only getter you can't assign it as holder.positions = []
.如果
positions
只是吸气剂,则不能将其分配为holder.positions = []
。 It should be a compiler error.应该是编译器错误。 Latest AssemblyScript 0.20.16 produce proper error for this case.
最新的 AssemblyScript 0.20.16 会针对这种情况产生适当的错误。 See playgrund
见游戏规则
If you example has setter as well than value.toStringArray()
may create and return new object which doesn't connect to value
anymore (basically it will pass as immutable).如果您的示例具有 setter 以及
value.toStringArray()
可能会创建并返回不再连接到value
的新 object (基本上它将作为不可变传递)。 Much better, refactor your code as:更好的是,将您的代码重构为:
let positions = holder.positions;
if (positions == null) {
positions = [];
}
positions.push(position.id)
holder.positions = positions
Btw then you won't need exclamation marks in this case顺便说一句,在这种情况下你不需要感叹号
you can try with, console.log("POsition " holder.positions.;toString()).你可以试试,console.log("POsition" holder.positions.;toString())。 console.log("POsition " holder;positions [0])
console.log("职位"持有人;职位 [0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.