简体   繁体   中英

Parse database Javascript How to get data from multiple tables when a column has same value

I am new with Parse DB. Recently I am trying to get data from multiple tables like I have the Users table and Employee table, where User.objectId is equal to Employee.userid. I have tried many ways but did not get any results.

let query = new Parse.Query(Employee);
query.descending("updatedAt");
let userQuery = new Parse.Query(User);
userQuery.select("username", "firstName", "lastName", "profile1", "reporter");
query.matchesKeyInQuery("reporter", "reporter", userQuery);

return userQuery.find({
   success: function (results) {
       return results;
       },
       error: function (error) {
            console.log(error);
       }
});

Is there any way to get data from multiple tables on Parse DB Javascript. I have tried to include User also but the query is blank.

If I understand your question correctly, you want to get both the User data and the Employee data with a single query. Per definition you cannot get "data from multiple tables" because each Parse query operates on exactly one table. However, you can include objects from other tables when they are stored as pointers on the queried table. Or you can run multiple queries in parallel and thus get data from multiple tables.

In your case, I suggest the first mentioned soltion, which requires a change of the data scheme: instead of just storing the userId in Employee you should rather store a pointer to User (probably named user ). This way you can run a query like this:

const query = new Parse.Query(Employee)
query.descending('updatedAt')
query.include('user')
const employee = await userQuery.first() // assuming that we have at least one Employee
const user = employee.get('user')

Please note that you cannot use the full set of query helpers with nested pointers, eg I think select won't work on employee.user .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM