解析数据库 Javascript 当列具有相同的值时,如何从多个表中获取数据



我是Parse DB的新手。最近,我尝试从多个表中获取数据,例如用户表和员工表,其中User.objectId等于Employee.userid。我尝试了很多方法,但没有得到任何结果。

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

有没有办法从解析数据库Javascript上的多个表中获取数据。我也尝试包括用户,但查询是空白的。

如果我正确理解您的问题,您希望通过单个查询同时获取User数据和Employee数据。根据定义,您无法获取"来自多个表的数据",因为每个 Parse 查询只对一个表进行操作。但是,当其他表中的对象作为指针存储在查询的表上时,您可以include这些对象。或者,您可以并行运行多个查询,从而从多个表中获取数据。

在您的情况下,我建议使用第一个提到的 soltion,这需要更改数据方案:与其将userId存储在Employee中,不如存储指向User的指针(可能名为 user (。这样,您可以运行如下所示的查询:

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')

请注意,您不能将全套查询助手与嵌套指针一起使用,例如我认为select不适用于employee.user

最新更新