Query on pointer in parse.com Objects in Javascript

本小妞迷上赌 提交于 2019-12-08 14:49:11

问题


I have a Class of Company which has User pointers. The query I want on Company class is like this:

Retrieve Company rows where User object has a name equal to 'ABC'

So, how should I form this query ?

var Company = Parse.Object.extend("Company");
var query = Parse.Query(Company);
query.include("User");

query.equalTo("name")       ????

Is it possible to write such a request in a single query ? Thanks.


回答1:


You'll need to query for the User first based on the name of "ABC". Then in the success callback of that query, do the query on your Company table using the objectId returned from the User query. Something like this:

var userQuery = Parse.Query('_User');
var companyQuery = Parse.Query('Company');

userQuery.equalTo('name', 'ABC');

userQuery.find({
  success: function(user) {
    var userPointer = {
      __type: 'Pointer',
      className: '_User',
      objectId: user.id
    }

    companyQuery.equalTo('user', userPointer);

    companyQuery.find({
      success: function(company) {
        // WHATEVER
      },
      error: function() {
      }
    });
  },
  error: function() {
  }
});



回答2:


You can use an inner query:

var Company = Parse.Object.extend("Company");
var mainQuery = Parse.Query(Company);

var UserObject = Parse.Object.extend("User");
var innerUserQuery = new Parse.Query(UserObject);
innerBankQuery.equalTo("name", "ABC");
mainQuery.matchesQuery("bank", innerBankQuery);

var ansCollection = mainQuery.collection();
    ansCollection.fetch({
        success: function(results) {
         // Do whatever ...
      }
    });



回答3:


I should say the query will be...

const userQuery = Parse.Query('_User');

userQuery.equalTo('name', 'ABC');

userQuery.find().then((user) => {

    const companyQuery = Parse.Query('Company');

    companyQuery.equalTo('user', {
      __type: 'Pointer',
      className: '_User',
      objectId: user.id
    });

    companyQuery.find().then((company) => {
        console.log(company);
    });
});



回答4:


Assuming Your Company collection has a field called User and User collection has a name field, then you can search for Company with user name through companyQuery.equalTo("User.name");

This works in Parse 2.1.0



来源:https://stackoverflow.com/questions/26923172/query-on-pointer-in-parse-com-objects-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!