Ionic framework 离子2存储-添加记录

Ionic framework 离子2存储-添加记录,ionic-framework,nosql,Ionic Framework,Nosql,我是NoSQL世界的新手,由于Ionic 2默认支持简单的键值DB,我在这里提供了一些帮助 我的应用程序有一个非常大的表单。如何保存新记录?如何检索这些特定记录 目前,为了保存新记录,我正在执行以下操作: save(data){ let newData = JSON.stringify(data); this.storage.set('reports', newData); } getData() { return this.storage.get('reports')

我是NoSQL世界的新手,由于Ionic 2默认支持简单的键值DB,我在这里提供了一些帮助

我的应用程序有一个非常大的表单。如何保存新记录?如何检索这些特定记录

目前,为了保存新记录,我正在执行以下操作:

save(data){
    let newData = JSON.stringify(data);
    this.storage.set('reports', newData);
}
getData() {
    return this.storage.get('reports');  
}
问题是它会覆盖记录,而不是插入新记录

我正在检索这样的记录:

save(data){
    let newData = JSON.stringify(data);
    this.storage.set('reports', newData);
}
getData() {
    return this.storage.get('reports');  
}
如何使用存储的JSON中的特定值获取特定记录


谢谢。

您需要做的是将报表作为一个数组,并将其设置为存储。 每次需要插入新记录时,请执行以下操作:

function(newData){
    var some_variable = storage.get('reports'); //get Existing Table
    some_variable.push(newData); //Inserts the new record to array
    storage.set('reports', some_variable); //Saves report with updated data
}
对于单独获取特定报告,我希望您有一些id或唯一属性y,以便能够区分报告。假设您的报告json如下所示:

var report {id : "UniqueID", name : "A sample report json"}
然后为了得到报告

function(reportId){
    var reports = this.storage.get('reports');//fetches your reports Array
    var wantedReport = {};//Variable to store the wanted report
    reports.forEach(function(r){ //Looping the array.You can use a forloop as well
        if(r.id === reportId){ //filtering for the wanted reportId
            wantedReport = r; // storing the report to variable
        }
    })
    return wantedReport; //Returning the report to the caller.
}

或者,如果您习惯使用Sql并希望使用类似Sql的方式存储这些数据,则可以安装并将数据存储在Sqlite DB中

您需要做的是将报告作为一个数组,并将其设置为存储。 每次需要插入新记录时,请执行以下操作:

function(newData){
    var some_variable = storage.get('reports'); //get Existing Table
    some_variable.push(newData); //Inserts the new record to array
    storage.set('reports', some_variable); //Saves report with updated data
}
对于单独获取特定报告,我希望您有一些id或唯一属性y,以便能够区分报告。假设您的报告json如下所示:

var report {id : "UniqueID", name : "A sample report json"}
然后为了得到报告

function(reportId){
    var reports = this.storage.get('reports');//fetches your reports Array
    var wantedReport = {};//Variable to store the wanted report
    reports.forEach(function(r){ //Looping the array.You can use a forloop as well
        if(r.id === reportId){ //filtering for the wanted reportId
            wantedReport = r; // storing the report to variable
        }
    })
    return wantedReport; //Returning the report to the caller.
}

或者,如果您习惯使用Sql并希望使用类似Sql的方式存储这些数据,则可以安装并将数据存储在Sqlite DB中

太好了,谢谢。考虑到我的关系数据库背景,我对使用SQL更为满意,但爱奥尼亚2不再支持WebSQL,它已被弃用,很快浏览器将不再支持它。虽然我最终将部署iOS,但我想坚持使用@ionic/storage,这是一种跨平台的方法,但要求您使用NoSQL。只有在浏览器中进行测试时,ionic才会默认使用webSql。。如果您已经安装了Sqlite cordova插件,那么当您在手机上运行应用程序时,您会在应用程序中获得一个实际的Sqlite数据存储。。但我想,一旦你通过了NoSql学习曲线,ionic/storage就非常简单和容易学习了。:-)你能看看这个吗?谢谢,太好了,谢谢。考虑到我的关系数据库背景,我对使用SQL更为满意,但爱奥尼亚2不再支持WebSQL,它已被弃用,很快浏览器将不再支持它。虽然我最终将部署iOS,但我想坚持使用@ionic/storage,这是一种跨平台的方法,但要求您使用NoSQL。只有在浏览器中进行测试时,ionic才会默认使用webSql。。如果您已经安装了Sqlite cordova插件,那么当您在手机上运行应用程序时,您会在应用程序中获得一个实际的Sqlite数据存储。。但我想,一旦你通过了NoSql学习曲线,ionic/storage就非常简单和容易学习了。:-)你能看看这个吗?谢谢