如何将数据写回存储?
Posted
技术标签:
【中文标题】如何将数据写回存储?【英文标题】:How to write data back to storage? 【发布时间】:2021-07-17 00:57:09 【问题描述】:我有一个名为changePlaceName
的方法,我知道它正在工作,但是在我调用getPlaces
查看更改后,我没有看到新的地名,而是在创建新地点时看到了名称。
这是changePlaceName
export function changePlaceName(placeId: u32, placeName: PlaceName): void
assert(placeId >= 0, 'Place ID must be >= 0');
const place = Place.find(placeId);
logging.log(place.name); //gives "Galata Tower"
place.name = placeName;
logging.log(place.name); // gives "New Galata Tower"
我需要以某种方式保存它,但我不知道该怎么做。
我也试过这种方式;
export function changePlaceName(placeId: u32, placeName: string): void
assert(placeId >= 0, 'Place ID must be >= 0');
const place = Place.find(placeId);
logging.log(place.name);
place.name = placeName;
let newPlace = storage.get<string>(placeName, 'new galata tower');
storage.set<string>(placeName, newPlace);
logging.log('New place is now: ' + newPlace);
现在我的可视化代码抱怨storage.set
中的newPlace
我该如何解决?
【问题讨论】:
【参考方案1】:Place.find
的代码是什么?我假设您在后台使用持久性地图。
有Place.set
吗?您需要将 Place 存储回用于查找它的同一密钥。
【讨论】:
【参考方案2】:因为您正在使用某种类来管理“地点”的概念,为什么不在更改名称后将该类的实例方法添加到save()
地点?
顺便说一下,如果您还在这里发布了Place
的代码,将会有所帮助
我猜它看起来像这样?
!注意:这是未经测试的代码
@nearBindgen
class Place
private id: number | null
private name: string
static find (placeId: number): Place
// todo: add some validation for placeId here
const place = places[placeId]
place.id = placeId
return place
// here is the instance method that can save this class
save(): bool
places[this.id] = this
// a collection of places where placeId is the index
const places = new PersistentVector<Place>("p")
【讨论】:
谢谢谢里夫和威廉。我现在可以将数据保存回存储器!以上是关于如何将数据写回存储?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 pyspark 将经过火花转换的数据写回 kafka 代理?