【发布时间】:2017-10-20 05:42:15
【问题描述】:
在挣扎了很多天后,我设法从 Parse Cloud Query 中检索到我想要的结果。
我的问题是我有两个查询合并到一个,所以需要使用不同的列来显示。
我将 Ionic3/Angular 4 与 Parse Server 一起使用。
这是我的 Parse Cloud 代码
Parse.Cloud.define('FriendsQuery', function (req, res) {
const fromUserQ = new Parse.Query("Friends")
.equalTo("toUser", req.user)
.include('fromUser')
.find();
const toUserQ = new Parse.Query("Friends")
.equalTo("fromUser", req.user)
.include('toUser')
.find();
Parse.Promise.when(toUserQ, fromUserQ)
.then((toUsers, fromUsers) =>
res.success({
toUsers: toUsers.map(e => e.get('toUser')),
fromUsers: fromUsers.map(e => e.get('fromUser')),
}))
.catch(res.error);
});
满足.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-meet',
templateUrl: 'meet.html'
})
export class MeetPage {
currentUser = Parse.User.current();
query = new Parse.Query(Parse.User);
tmp =[];
tmp2 =[];
friends: any=[];
initializeItems() {
this.friends = this.localdata.tmpfriends;
}
retrieveFriends(){
if (this.currentUser= Parse.User.current()) {
console.log(this.currentUser.id)
Parse.Cloud.run('FriendsQuery',{currentuser: this.currentUser.id}).then(
res => {
console.log(res);
this.tmp2 = res;
this.localdata.setFriends(this.tmp2);
this.friends = this.localdata.tmpfriends;
console.log(this.friends);
});
}
}
constructor(public navCtrl: NavController, private localdata: localData, private dataService: Data) {
this.searchControl = new FormControl;
}
showFriends: any = false;
searchControl: FormControl;
searchTerm: string = '';
searching: any = false;
filterfriends(searchTerm){
return this.friends.filter((friend) => {
return friend.fromUser.username.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
});
}
ionViewDidLoad() {
this.retrieveFriends();
this.setFilteredItems();
this.searchControl.valueChanges.debounceTime(700).subscribe(search => {
this.searching = false;
this.setFilteredItems();
});
}
onSearchInput(){
this.searching = true;
}
setFilteredItems() {
this.initializeItems();
this.friends = this.filterfriends(this.searchTerm);
}
onCancel(ev: any) {
this.initializeItems();
}
}
meet.html
<ion-header>
<ion-navbar>
<ion-searchbar [(ngModel)]="searchTerm" [formControl]="searchControl" (ionCancel)="onCancel($event)" (ionInput)="onSearchInput()">
</ion-searchbar>
</ion-navbar>
</ion-header>
<ion-content padding>
<div *ngIf="searching" class="spinner-container">
<ion-spinner></ion-spinner>
</div>
<ion-item class="item-avatar item-avatar-left item-button-right common-list" >
<ion-avatar *ngIf="friend.fromUser.objectId == currentUser.id" item-left><img style="left:0px !important;margin-top:0px !important;" [src]="friend.toUser.avatar || '/assets/img/no-avatar.png'"/></ion-avatar>
<ion-label *ngIf="friend.fromUser.objectId == currentUser.id">{{friend.toUser.username}}</ion-label>
<ion-avatar *ngIf="friend.toUser.objectId == currentUser.id" item-left><img style="left:0px !important;margin-top:0px !important;" [src]="friend.fromUser.avatar || '/assets/img/no-avatar.png'"/></ion-avatar>
<ion-label *ngIf="friend.toUser.objectId == currentUser.id">{{friend.fromUser.username}}</ion-label>
</ion-item>
</ion-content>
local.ts
import { Component } from '@angular/core';
export class localData {
setFriends (friends){
window.localStorage.friends_data = JSON.stringify(friends);
}
getFriends(){
return JSON.parse(window.localStorage.friends_data || '[]');
}
tmpfriends = this.getFriends();
}
在我的“朋友”表中,我有两列,“fromUser”和“toUser”,我想显示当前登录用户的朋友。所以在 Angular 代码中,我需要 friend.toUser.username 和 friend.fromUser.username 取决于登录的用户。
当我使用第一个时,我只获得向登录用户发送好友请求的用户的结果,反之亦然。
为了更清楚,我附上了图片链接。我不想要屏幕上的结果,而是我用箭头指向的结果。
使用 hmtl 代码我设法得到了这个结果,但是用这个过滤它们并不方便:
filterfriends(searchTerm){
return this.friends.filter((friend) => {
return friend.fromUser.username.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
});
}
因为我需要同时过滤friend.toUser和friend.fromUser
谢谢
【问题讨论】:
-
uh-oh:
if (this.currentUser= Parse.User.current())几乎可以肯定没有按照您的想法行事。试试这个:if(this.currentUser.id === Parse.User.current().id) -
OOOOH,我想我明白你想要什么了!!!将更新我的答案,让我们看看....
-
嗨亚瑟,是的,你上面提到的没有错,但是我将如何显示结果。对我来说,最好的选择是检查 Cloud Code,如果用户是 fromUser 或 toUser,以便检索最终可以在 hmtl 中作为friend.username使用的列表。我正在等待您的回复!再次感谢您!
标签: angular ionic-framework parse-platform parse-server