1

I have set the array of data into the storage with the key 'todo' in the data provider as

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';

@Injectable()
export class DataProvider {

  constructor(public storage: Storage) {
    console.log('Hello DataProvider Provider');
  }

  getData() {
    return this.storage.get('todos'); 
  }

  save(data){
    this.storage.set('todos', data);
  }

  remove(id){
    console.log("Removing data ID:",id);
    this.storage.remove(id);
  }


}

The data consists of an array with properties id, title, body as

{ id: 0, title: "lorem epsum", body: "lorem epsum" }
{ id: 1, title: "lorem epsum", body: "lorem epsum" }
{ id: 2, title: "lorem epsum", body: "lorem epsum" }
{ id: 3, title: "lorem epsum", body: "lorem epsum" }
{ id: 4, title: "lorem epsum", body: "lorem epsum" }

Now, what i am trying to do is to remove the array from the data. I want to remove an array with id 3

{ id: 3, title: "lorem epsum", body: "lorem epsum" }

I have used the storage function remove(key) to remove one array

But it is showing the error like

3 used as a key, but it is not a string.

I want to try with the title but the title of the item may not be unique. SO, i tried to remove the array using the id.

Any help will be appreciated.

Thank you.

4

2 回答 2

2

使用this.storage.remove(key).Key 是存储中数组元素的 ID

于 2018-03-23T13:55:08.313 回答
0

您的存储存储在哪里?你能在 Dom/Console 中显示你的数据吗?请记住,如果未设置您的数据,则默认计数始终从 0 开始,因此 0,1,2,3 其中 3 是第 4 行。因此,从这里看来,您正在删除键 3 并探测核心/键的键方法而不是删除第 3 行。删除的示例应如下所示:

this.storage.remove(a => a.id).where(a.id == 3);

或者正确的语法可能是:

  this.storage.remove(this.storage.key(id));

如果您向我们展示您的整个代码/课程,我们可以提供更好的建议。

**编辑*****

@Injectable()
export class DataProvider {

       constructor(public storage: Storage) {
       console.log('Hello DataProvider Provider');

   }

//Set/Instantiate data
var totalItems = this.storage.length,
stored = document.getElementById('stored'),
addButton = document.getElementById('add-button'),
subButton = document.getElementById('sub-button');

addButton.addEventListener('click', addToStorage);
subButton.addEventListener('click', removeStorage);

updateStorage(); // adds stored timestamps to #stored div

function updateStorage(){
    // Reset/update innerHTML for #stored div
    stored.innerHTML = "Stored Results<br>"

    for(item in this.storage) {
        var obj = JSON.parse(this.storage[item]);
        stored.innerHTML += obj.time + '<br>';
    }
}

function addToStorage(event) {
    // Create a new this.storage property and assign its value
    var propName = 'item' + totalItems;
    this.storage.setItem( propName, JSON.stringify( {'time': (event.timeStamp).toString(), 'target': (event.target).toString()} ) );
    totalItems = this.storage.length;

    // Add new value to #stored element
    var obj = JSON.parse(this.storage[propName]);
    stored.innerHTML += obj.time + '<br>';
}

function removeStorage() {
    // Remove the latest property from this.storage
    var propName = 'item' + (totalItems - 1);
    this.storage.removeItem(propName);
    totalItems = this.storage.length;

    // Update html to reflect changes
    updateStorage();
}


    }

这是我在其他地方找到的一个例子,主要区别可能是:

  1. 您没有先设置/实例化关键数据
  2. 如果可以,拉 ID 3,但此 ID 3 和密钥 3 之间可能存在冲突。
  3. 可能是无论您尝试删除的字符串是什么,并且您正在尝试删除一个键而不是 id: 3,都不允许删除键 3。

尝试这些示例,但也许查找更多“Ionic CRUD”示例,您可能会成功。希望这可以帮助!

于 2018-01-23T15:29:09.967 回答