未定义对象数组的离子存储
Posted
技术标签:
【中文标题】未定义对象数组的离子存储【英文标题】:Ionic Storage Undefined for Array Of Objects 【发布时间】:2018-03-19 22:37:17 【问题描述】:我正在努力获取从 Parse 服务器检索到的对象数组,但没有成功。
我正在使用 Ionic 3 和 Ionic Storage 构建应用程序。似乎我得到了一个承诺而不是价值,但我已经尝试了一切来解决它。
我的 search.ts 文件:
import Data from './../../services/data';
import localData from './../../services/local';
import Component from '@angular/core';
import NavController from 'ionic-angular';
import Parse from 'parse';
import 'rxjs/add/operator/map';
import User from '../../models/User';
import 'rxjs/add/operator/debounceTime';
import FormControl from '@angular/forms';
@Component(
selector: 'page-search',
templateUrl: 'search.html',
)
export class SearchPage
currentUser = Parse.User.current();
query1 = new Parse.Query('Friends');
friendQuery = new Parse.Query('Friends');
query = new Parse.Query(Parse.User);
tmp: any =[];
users: any= [];
user: any =[];
initializeItems()
this.users = this.dataService.tmpusers;
retrieveUsers()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('UserQuery').then(
res =>
console.log('user query')
this.tmp = res;
console.log(this.tmp);
this.dataService.setUsers2(this.tmp);
this.users = this.dataService.tmpusers;
console.log(this.users);
)
retrieveFriends()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('FriendsQuery', currentuser: 'fEjQgAPvDO').then(
res =>
console.log(res)
)
constructor(public navCtrl: NavController, private localdata: localData,
private dataService: Data)
this.searchControl = new FormControl;
showUsers: any = false;
searchControl: FormControl;
searchTerm: string = '';
searching: any = false;
filterusers(searchTerm)
return this.users.filter((user) =>
return user.username.toLowerCase().indexOf(searchTerm.toLowerCase())
> -1;
);
ionViewDidLoad()
this.retrieveFriends();
this.retrieveUsers();
this.setFilteredItems();
this.searchControl.valueChanges.debounceTime(700).subscribe(search =>
this.searching = false;
this.setFilteredItems();
);
onSearchInput()
this.searching = true;
setFilteredItems()
this.initializeItems();
this.users = this.filterusers(this.searchTerm);
onCancel(ev: any)
this.initializeItems();
还有我的 Data.ts 文件:
import Storage from '@ionic/storage';
import Injectable, Component from '@angular/core';
@Injectable()
export class Data
tmpusers = this.getUsers2();
constructor(public storage: Storage)
getUsers2()
this.storage.get('users');
setUsers2(users)
this.storage.set('users', users);
当我尝试运行它时,第一个控制台日志会返回用户数组,但我需要的第二个是未定义的。
我能做什么?
对大部分代码感到抱歉
非常感谢
最新代码
import Data from './../../services/data';
import localData from './../../services/local';
import Component from '@angular/core';
import NavController from 'ionic-angular';
import Parse from 'parse';
import 'rxjs/add/operator/map';
import User from '../../models/User';
import 'rxjs/add/operator/debounceTime';
import FormControl from '@angular/forms';
@Component(
selector: 'page-search',
templateUrl: 'search.html',
)
export class SearchPage
currentUser = Parse.User.current();
query1 = new Parse.Query('Friends');
friendQuery = new Parse.Query('Friends');
query = new Parse.Query(Parse.User);
tmp: any =[];
users: any= [];
user: any =[];
initializeItems()
this.users = this.dataService.newusers;
retrieveUsers()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('UserQuery').then(
res =>
console.log('user query')
this.tmp = res;
console.log(this.tmp);
this.dataService.setUsers2(this.tmp).then(()=>
this.dataService.getUsers2().then((val)=>
this.users = val;
console.log(this.users);
);
);
);
retrieveFriends()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('FriendsQuery', currentuser: 'fEjQgAPvDO').then(
res =>
console.log(res)
)
constructor(public navCtrl: NavController, private localdata: localData, private dataService: Data)
this.searchControl = new FormControl;
showUsers: any = false;
searchControl: FormControl;
searchTerm: string = '';
searching: any = false;
filterusers(searchTerm)
return this.users.filter((user) =>
return user.username.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
);
ionViewDidLoad()
this.retrieveFriends();
this.retrieveUsers();
this.setFilteredItems();
this.searchControl.valueChanges.debounceTime(700).subscribe(search =>
this.searching = false;
this.setFilteredItems();
);
onSearchInput()
this.searching = true;
//this code is to hide users until input is triggered
//if (this.searchTerm.length >=1)
// return this.showUsers= true;
// else
// return this.showUsers = false;
//
//end of code
setFilteredItems()
this.initializeItems();
this.users = this.filterusers(this.searchTerm);
onCancel(ev: any)
this.initializeItems();
//searchfriends()
// this.friendQuery = new Parse.Query.or(this.query1.equalTo('fromUser',Parse.User.current()),this.query1.equalTo('toUser',Parse.User.current()));
//let users = this.query.find();
//return this.users;
//
和data.ts
import Storage from '@ionic/storage';
import Injectable, Component from '@angular/core';
@Injectable()
export class Data
newusers = [];
tmpusers = this.getUsers2();
constructor(public storage: Storage)
getUsers2(): Promise<any>
return this.storage.get('users');
setUsers2(users): Promise<any>
return this.storage.set('users', users);
旧代码工作
import Data from './../../services/data';
import localData from './../../services/local';
import Component from '@angular/core';
import NavController from 'ionic-angular';
import Parse from 'parse';
import 'rxjs/add/operator/map';
import User from '../../models/User';
import 'rxjs/add/operator/debounceTime';
import FormControl from '@angular/forms';
@Component(
selector: 'page-search',
templateUrl: 'search.html',
)
export class SearchPage
currentUser = Parse.User.current();
query1 = new Parse.Query('Friends');
friendQuery = new Parse.Query('Friends');
query = new Parse.Query(Parse.User);
tmp: any =[];
users: any= [];
user: any =[];
initializeItems()
this.users = this.localdata.tmpusers;
retrieveUsers()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('UserQuery').then(
res =>
console.log('user query')
this.tmp = res;
console.log(this.tmp);
this.localdata.setUsers(this.tmp);
this.users = this.localdata.tmpusers;
console.log(this.users);
);
retrieveFriends()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('FriendsQuery', currentuser: 'fEjQgAPvDO').then(
res =>
console.log(res)
)
constructor(public navCtrl: NavController, private localdata: localData, private dataService: Data)
this.searchControl = new FormControl;
showUsers: any = false;
searchControl: FormControl;
searchTerm: string = '';
searching: any = false;
filterusers(searchTerm)
return this.users.filter((user) =>
return user.username.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
);
ionViewDidLoad()
this.retrieveFriends();
this.retrieveUsers();
this.setFilteredItems();
this.searchControl.valueChanges.debounceTime(700).subscribe(search =>
this.searching = false;
this.setFilteredItems();
);
onSearchInput()
this.searching = true;
//this code is to hide users until input is triggered
//if (this.searchTerm.length >=1)
// return this.showUsers= true;
// else
// return this.showUsers = false;
//
//end of code
setFilteredItems()
this.initializeItems();
this.users = this.filterusers(this.searchTerm);
onCancel(ev: any)
this.initializeItems();
//searchfriends()
// this.friendQuery = new Parse.Query.or(this.query1.equalTo('fromUser',Parse.User.current()),this.query1.equalTo('toUser',Parse.User.current()));
//let users = this.query.find();
//return this.users;
//
和local.ts
import Component from '@angular/core';
export class localData
setUsers (users)
window.localStorage.users_data = JSON.stringify(users);
getUsers()
return JSON.parse(window.localStorage.users_data || '[]');
tmpusers = this.getUsers();
constructor()
this.tmpusers.sort(function(a, b)
var nameA = a.username.toUpperCase(); // ignore upper and lowercase
var nameB = b.username.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB)
return -1;
if (nameA > nameB)
return 1;
return 0;
);
【问题讨论】:
second one
是什么意思?
console.log(this.users);
【参考方案1】:
您不能像那样访问该值,因为这些是Async
操作。您可以尝试如下所示。
data.ts
getUsers2(): Promise<any>
retrun this.storage.get('users');
setUsers2(users): Promise<any>
return this.storage.set('users', users);
search.ts
retrieveUsers()
if (this.currentUser= Parse.User.current())
Parse.Cloud.run('UserQuery').then(
res =>
console.log('user query')
this.tmp = res;
console.log(this.tmp);
this.dataService.setUsers2(this.tmp).then(()=>
this.dataService.getUsers2().then((val)=>
this.users = val;
console.log(this.users);
);
);
);
【讨论】:
hhmm 不错的方法。但我有这个错误:Typescript Error Property 'then' does not exist on type 'void'。 您需要更改方法返回类型,如上所述。你做了吗? 我没有实现:tmpusers = this.getUsers2();现在,没有错误,但列表为空 试试我的方法看看。查看服务器和存储返回的值。 我完全实现了您发送的内容。似乎它无法访问表的属性用户名,所以这就是空数组的原因。在 search.html 我有 *ngfor 循环和 user.username 我在图像中发送的是 console.log。它应该返回类的所有列,但它只返回 Id以上是关于未定义对象数组的离子存储的主要内容,如果未能解决你的问题,请参考以下文章
对象数组:无法读取 onchange 事件中未定义的属性“id”