如何使用城市名称搜索离子存储并获取城市 ID

How to search ionic storage for with city name and get city id

我需要帮助,我想在 "citiesData" 中搜索离子本地存储,并在用户通过地理定位 returns 城市名称定位时返回 city_id。

目前,我的城市数据在初始页面加载时从服务器加载并保存在本地存储中。在下一个页面加载时,我们使用本地存储数据

    getCitiesData() {
        return new Promise((resolve, reject) => {
            this.storage.get('token').then((value) => {
                let headers = new Headers();
                headers.append('Content-Type', 'application/json');
                headers.append('Authorization', 'Bearer ' + value);

                this.http
                    .get(ApiUrl + '/getCities', { headers: headers })
                    .pipe(map((res) => res.json()))
                    .subscribe(
                        (data) => {
                            this.cities = data.data;
                            this.storage.set('citiesData', data.data);
                            resolve(data);
                        },
                        (error) => {
                            reject(error);
                        }
                    );
            });
        });
    }

我想得到 company_id 和 city_id 当用户地理位置 returns 城市名称。这将在通过 api 调用发送到后端时使用。

您应该将每个元素作为键值对添加到存储中,例如

for(city in cities){
   storage.set(city.key, city.id) 
}

然后您可以使用

检索特定的 ID
storage.get(city.key) //Returns the city.id

我最终将城市名称发送到后端并在 API post 上进行了搜索以获取城市 ID 并将该变量分配给所需的 city_id 字段