2

我有一个名为的方法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);
}

现在我的视觉代码正在抱怨newPlace内部storage.set

我如何解决它?

4

2 回答 2

2

的代码是Place.find什么?我假设您在后台使用持久性地图。

Place.set吗?您需要将 Place 存储回用于查找它的同一密钥。

于 2021-04-22T20:24:29.067 回答
1

因为您正在使用某种类来管理“Place”的概念,为什么不在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")

于 2021-04-22T20:29:41.947 回答