Ionic3与sqllite- on选择 [英] Ionic3 with sqllite- on select

查看:115
本文介绍了Ionic3与sqllite- on选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用android中的cordova查询来自SQL Lite DB的记录时遇到问题

I have issue with querying the records from SQL Lite DB using cordova in android

this.platform.ready().then(() => {
          this.sqlite.create({
            name: 'temp.db',
            location: 'default'
          }).then((db: SQLiteObject) => {

           console.log('Querying for temp user '+user.userName+'Password '+user.password);

           console.log('User queried'+user.userName);

            db.executeSql("SELECT * FROM USER  where USER_NAME = ? and USER_PWD=?", [user.userName,password]).then(
            response  => {
              let records='';
              for (let i = 0; i < response.rows.length; i++) {
                records = records+ JSON.stringify(response.rows.item(i))+'\n'; //Prints row correctly
              }
             this._util.presentAlert('Records selected like from- USR-',records);
            })
         .catch(
            e => this._util.presentAlert('Fail- Select like from- USER-Temp DBUSER',e));

            db.executeSql("SELECT * FROM USER  where USER_NAME = ? and USER_PWD=? ", [user.userName,password ]).then(
               response => {
                if (response && response.rows && response.rows.length > 0) {


                    for (let i = 0; i < response.rows.length; i++) {
                            let access = { 
                                firstName :response.rows.item[i].FIRST_NAME, //This is undefined.
                                lastName :response.rows.item[i].LAST_NAME,
                                userName:response.rows.item[i].USER_NAME,
                                userId:response.rows.item[i].USER_ID
                              }

                             observer.next(access);

                      }

                      observer.complete();

                } else {
                  let access = {status:'Fail',msg:'Bad credentials for Temp DB login'};
                 console.log('No record for the user from- USER'+user.userName);
                  observer.next(access);
                  observer.complete();               

                }
              })
              .catch(
              e => {
               console.log('Fail- Select query gone wrong * from- USER FOR Temp DB LOGIN' + e);
                let access = {status:'Fail',msg:'Bad credentials for Temp DB login'};
                observer.next(access);
                observer.complete();

              });

问题是这是正确打印记录

The issue is this one is printing the records correctly

JSON.stringify(response.rows.item(i))
O/P
{'USER_ID':1,'FIRST_NAME':'Temp','LAST_NAME':'User','USER_NAME':'TEMPUSER','USER_PWD':'TEMPPWD'}

下面是抛出未定义的错误

Below is throwing undefined error

firstName :response.rows.item[i].FIRST_NAME
Fail- Select query gone wrong * from- USER FOR Temp DB LOGIN TypeError: Cannot read property 'FIRST_NAME' of undefined

有人有这个问题吗?我不知道为什么我无法像JSON那样获取它。
请指教

Anybody got this issue? I am not sure why I am unable to fetch it as as JSON. Please advise

推荐答案

这就是我解决问题的方法。如果它可以帮助任何人

This is how I resolved the issue. Incase if it helps anyone

1.Removed cordova sqllite plugin
2.Reinstalled Ionic sqllite plugin -latest
3.Took npm update of cordova
4.Performed cordova prepare android
5.Executed ionic build android
6.Corrected the typo from the queries response.rows.item(i).FIRST_NAME. It should be curly brace only. Sadly IDE doesnt trigger any error

这篇关于Ionic3与sqllite- on选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆