【发布时间】:2017-10-08 00:25:13
【问题描述】:
我在 Android 中使用 cordova 从 SQL Lite DB 查询记录时遇到问题
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();
});
问题是这个打印记录正确
JSON.stringify(response.rows.item(i))
O/P
{'USER_ID':1,'FIRST_NAME':'Temp','LAST_NAME':'User','USER_NAME':'TEMPUSER','USER_PWD':'TEMPPWD'}
下面是抛出未定义的错误
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 格式获取它?
【问题讨论】:
-
尽管您的查询中有
password,您能否再试一次user.password? -
实际上没有加密密码。所以我使用了一个新变量。但是如果你看到这里当我警告下面的语句时它工作正常并给出 json。记录+ JSON.stringify(response.rows.item(i))+'\n'; .问题仅在我使用 :response.rows.item[i].FIRST_NAME 时出现
-
console.log('用户在数据库中找到-->响应项类型'+typeof(response.rows.item[i]));将类型设为未定义。
标签: android sqlite cordova angular ionic3