如何序列化 PersistentMap 和 return 它
How to serialize PersistentMap and return it
我有一个简单的 map 并想添加到它并 return 它序列化。
这是我的代码
organizations: PersistentMap<string, string> = new PersistentMap<
string,
string
>("oc");
getOrgList(): PersistentMap<string, string> {
return this.organizations;
}
合约调用panic如下:
kind: {
ExecutionError: 'Smart contract panicked: Cannot parse JSON, filename: "~lib/assemblyscript-json/decoder.ts" line: 144 col: 5'
},
更新:
当地图为空时,我不会收到任何错误。
我使用
将项目添加到地图
this.organizations.set(orgCode, orgName);
然后我就这样从命令行调用它
near call cert.msaudi.testnet createOrgId '{\"orgCode\":\"AAA\",\"orgName\":\"AAAA\"}' --accountId=msaudi.testnet
编辑:完整合约代码
import { Context, PersistentMap } from "near-sdk-core";
@nearBindgen
export class Contract {
public organizations: PersistentMap<string, string> = new PersistentMap<
string,
string
>("oc"); //orgCode, orgName
//check if organization name is there from the frontend to save processing time.
@mutateState()
createOrgId(orgCode: string, orgName: string): string {
this.organizations.set(orgCode, orgName);
return "Organization created with code:" + orgCode + " name: " + orgName;
}
getOrgList(): PersistentMap<string, string> {
return this.organizations;
}
}
如果你想从合约return一个正规的地图,你需要将地图从PersistentMap<string,string>
解析为一个正规的Map<string,string>
。不仅如此,如果您想 return 除键之外的所有值,还需要与地图分开跟踪键。那是因为 PersistentMap
不存储密钥,您无法检索它们 (Here's a separate thread about it)。
keys: PersistentVector<string>; // Need to store the keys as well
getOrgList(): Map<string, string> {
const res: Map<string, string> = new Map<string, string>();
for (let i = 0; i < this.keys.length; i++) {
res.set(this.keys[i], this.organizations.getSome(this.keys[i]));
}
return res;
}
我有一个简单的 map
organizations: PersistentMap<string, string> = new PersistentMap<
string,
string
>("oc");
getOrgList(): PersistentMap<string, string> {
return this.organizations;
}
合约调用panic如下:
kind: {
ExecutionError: 'Smart contract panicked: Cannot parse JSON, filename: "~lib/assemblyscript-json/decoder.ts" line: 144 col: 5'
},
更新: 当地图为空时,我不会收到任何错误。 我使用
将项目添加到地图this.organizations.set(orgCode, orgName);
然后我就这样从命令行调用它
near call cert.msaudi.testnet createOrgId '{\"orgCode\":\"AAA\",\"orgName\":\"AAAA\"}' --accountId=msaudi.testnet
编辑:完整合约代码
import { Context, PersistentMap } from "near-sdk-core";
@nearBindgen
export class Contract {
public organizations: PersistentMap<string, string> = new PersistentMap<
string,
string
>("oc"); //orgCode, orgName
//check if organization name is there from the frontend to save processing time.
@mutateState()
createOrgId(orgCode: string, orgName: string): string {
this.organizations.set(orgCode, orgName);
return "Organization created with code:" + orgCode + " name: " + orgName;
}
getOrgList(): PersistentMap<string, string> {
return this.organizations;
}
}
如果你想从合约return一个正规的地图,你需要将地图从PersistentMap<string,string>
解析为一个正规的Map<string,string>
。不仅如此,如果您想 return 除键之外的所有值,还需要与地图分开跟踪键。那是因为 PersistentMap
不存储密钥,您无法检索它们 (Here's a separate thread about it)。
keys: PersistentVector<string>; // Need to store the keys as well
getOrgList(): Map<string, string> {
const res: Map<string, string> = new Map<string, string>();
for (let i = 0; i < this.keys.length; i++) {
res.set(this.keys[i], this.organizations.getSome(this.keys[i]));
}
return res;
}