简体   繁体   English

风帆的JavaScript灰烬

[英]JavaScript Ember with Sails

I'm working with EmberJS and SailsJS. 我正在使用EmberJS和SailsJS。 Now I've been asked to make a statistics page and handle the filtering process in the SailsJS. 现在,我被要求制作一个统计页面并处理SailsJS中的过滤过程。

I have a model for departments and another model for requests the relationship between these models is (request belongsTo department). 我有一个部门模型,另一个请求这些模型之间关系的模型是(请求belongsTo部门)。 For some reason my manager prevented me to make a (hasMany) relationship. 由于某种原因,我的经理阻止我建立(有很多)关系。

Now what I want to do is to loop through all the departments and store them in new Object, inside that loop I want to loop through all the requests by Using Request.count({where : { department : department.id }}) and get the number of requests for each department in the departments Array Of Object. 现在,我要做的是遍历所有部门并将其存储在新对象中,在此循环中,我想使用Request.count({where : { department : department.id }})遍历所有请求在部门数组对象中获取每个部门的请求数。

I tried to do it as I explained but the problem is when I log the department inside the request loop it gives me the result as I imagined but when I add (.id) it shows me 'undefined'. 我试图按照我的解释去做,但是问题是当我在请求循环中记录部门时,它给了我想像的结果,但是当我添加(.id)时,它显示了我“未定义”。

Here is my code: 这是我的代码:

Department.find().then((departments) => {
    report.departments = departments;
    Request.count({ department : departments.id}).exec(function countMe(err, count) {
       console.log(count);
    })
})

PS: if there's any other approach for this task please tell me, I'm kind of a beginner. PS:如果有其他方法可以完成此任务,请告诉我,我是个初学者。

Your departments object in the .then callback is an array of department objects. 你的departments对象的.then回调部门对象的数组。 To get an id you'd need to do something like departments[0].id . 要获取ID,您需要执行诸如departments[0].id

I might not recommend using .count because that means a separate trip to the database for each department. 我可能不建议使用.count因为这意味着每个部门都要单独访问数据库。 Sails waterline may have some way for you to count up Requests grouped by department id, but just to get a steamroller working example, I would first just get everything and do some processing in your code: Sails水线可能会为您提供一些按部门ID分组的请求的方法,但是为了获得Steamroller的工作示例,我首先要获取所有信息并在您的代码中进行一些处理:

Department.find().then((departments) => {
    Request.find().then((requests) => {
        var requestCounts = {}; // we'll store the counts in this object
        for (var idx = 0; idx < requests.length; idx++) {
            if (!requestCounts[requests[idx].department]) {
                requestCounts[requests[idx].department] = 0;
            }
            requestCounts[requests[idx].department]++;
        }
        // use requestCounts...
    });
});

Creating a separate object like that may not be what you want to do, but something like this should serve whatever purpose you have. 像这样创建一个单独的对象可能不是您想要做的,但是这样的事情应该可以满足您的任何目的。 Notice, the code I wrote did not require finding all the department objects, but whatever you are doing likely will... 请注意,我编写的代码不需要查找所有部门对象,但是您所做的任何事情都可能...

If you're interested in efficiency, then once you get this working, you can see if there is some way you can query the database to directly get the counts you want instead of this in-code processing. 如果您对效率感兴趣,那么一旦开始工作,就可以查看是否有某种方法可以查询数据库以直接获取所需的计数,而不用进行代码内处理。 But I would start with something simple like this. 但是我将从这样简单的事情开始。


EDIT: 编辑:

It sounds like you may be able to use something like: 听起来您可能可以使用类似:

Request.find().groupBy('department').sum('count').exec(function (err, results){
    console.log(results);
});

But I'm finding conflicting reports on whether this works with sails-mongo, so take this as a "maybe this will work" recommendation. 但是我发现有关此方法是否适用于sails-mongo的报道相互矛盾,因此将其作为“也许可行”的建议。

I assume Request has a property department which is an ObjectId related to departments. 我假设Request具有一个属性部门,该属性部门是与部门相关的ObjectId。

The quick one is to correct the following: 快速的方法是更正以下内容:

  • the 2nd line does not make sense as reports is not used 第二行没有意义,因为未使用报告
  • do

    let departmentIds = _.map(departments, 'id'); 让departmentIds = _.map(departments,'id');

and use departmentIds in the count query object 并在count查询对象中使用departmentIds

However, you are anyway finding all departments in the initial query, I would assume you count all the Requests anyway. 但是,无论如何,您都将在初始查询中找到所有部门,但我还是假设您仍在计算所有请求。 Finding the departments first only makes sense in case you have query object there limiting the number of returned departments. 只有在查询对象在那里限制返回的部门数的情况下,首先找到部门才有意义。

In addition, if you know the departmentIds in question you might not need to query them. 另外,如果您知道有问题的部门编号,则可能不需要查询它们。 Otherwise you might use a projection to just return the departmentIds instead of all the properties of all the departments 否则,您可能会使用投影仅返回departmentIds而不是所有部门的所有属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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