解析云代码定义结果 [英] Parse Cloud Code Define Results

查看:105
本文介绍了解析云代码定义结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多日努力,我设法从Parse Cloud Query中检索出我想要的结果。



我的问题是我有两个查询与一个查询,所以需要使用diffurent列来显示。



我正在使用Ionic3 / Angular 4和Parse Server。



这是我的Parse Cloud Code

  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);
});

meets.ts

 从'./../../services/data'导入{数据}; 
从'./../../services/local'导入{localData};来自'@ angular / core'的
import {Component};来自'ionic-angular'的
import {NavController};
从'parse'导入{Parse};
import'rxjs / add / operator / map';
从'../../models/User'导入{User};
import'rxjs / add / operator / debounceTime';来自'@ angular / forms'的
import {FormControl};

@Component({
selector:'page-meet',
templateUrl:'meet.html'
})
导出类MeetPage {

currentUser = Parse.User.current();
query = new Parse.Query(Parse.User);
tmp = [];
tmp2 = [];
朋友: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})。然后(
res => {
console.log(res);
this.tmp2 = res;
this.localdata.setFriends(this.tmp2);
this.friends = this.localdata.tmpfriends;
console.log(this.friends);
});
}
}

构造函数(public navCtrl:NavController,private localdata:localData,private dataService:Data){
this.searchControl = new FormControl;
}

showFriends:any = false;
searchControl:FormControl;
searchTerm:string ='';
搜索:any = false;

filterfriends(searchTerm){
返回this.friends.filter((朋友)=> {
返回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 =searchclass =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.iditem-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.iditem-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

 从'@ angular / core'导入{Component}; 

导出类localData {

setFriends(friends){
window.localStorage.friends_data = JSON.stringify(friends);
}
getFriends(){
返回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((朋友) => {
return friend.fromUser.username.toLowerCase()。indexOf(searchTerm.toLowerCase())> -1;
});
}

因为我需要过滤friend.toUser和friend.fromUser



截图



谢谢

解决方案

您不需要整个 {__ type :Pointer,className:_ User,objectId:req.params.currentuser} if toUser fromUser 已经是用户指针,你可以只做 .equalTo(toUser,request.params.currentUser); 。此外,如果您是从登录的客户端发出此请求,则可以执行 .equalTo(toUser,request.user);



还有一个查询方法 .include(< key>); 将包含指向的对象。所以,你可以做 friendQuery.include(toUser)。include(fromUser); 并获取该信息。并且,如果你的用户有一些额外的指针,比如一个ContactInfo类,这适用于点表示法。您可以执行 query.include(toUser.contactInfo); ,并且该对象也将被包含。



<除此之外,我不完全确定你遇到的问题是什么。什么是你想工作的不工作?听起来你得到了你想要的信息。


after struggling many days I managed to retrieve the results I want from a Parse Cloud Query.

My problem is that I have two queries combouned to one, so there is need to use diffurent column to display.

I am using Ionic3/Angular 4 with Parse Server.

Here is my Parse Cloud Code

    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);
});

meets.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();
   }    

In my table "Friends" I have two columns, "fromUser" and "toUser" and I want to display the friends of a current logged in user. So in Angular code i need both friend.toUser.username and friend.fromUser.username depending the logged in user.

When I use the first one I get only the results for the users that sent a friend request to the logged in user and the opposite.

I attach an image link to be more clear. I don`t want the results in the screen but the ones that I point with the arrows.

With the hmtl code I managed to get this results but it is not convinienent to filter them with this one:

filterfriends(searchTerm){ 
return this.friends.filter((friend) => {
return friend.fromUser.username.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
});    
 }

Because I need to filter both friend.toUser and friend.fromUser

screenshot

Thank you

解决方案

You don't need the whole { "__type": "Pointer", "className": "_User", "objectId": req.params.currentuser } if toUser and fromUser are already user pointers, you can just do .equalTo("toUser", request.params.currentUser);. Additionally, if you're making this request from a client that is logged in, you can just do .equalTo("toUser", request.user);

There is also a query method, .include(<key>); that will include the pointed to object. So, you could do friendQuery.include("toUser").include("fromUser"); and have that information fetched. And, this works with dot notation, if you have some sort of additional pointer on your users, say, a ContactInfo class. You could do query.include("toUser.contactInfo");, and that object will be included as well.

Beyond this I'm not entirely sure what the issue you're having is. What isn't working that you want to work? It sounds like you're getting the information you wanted.

这篇关于解析云代码定义结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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