比较两次API调用的结果,并在MEAN应用中返回它们的差值 [英] Comparing results from two API calls and returning their difference in MEAN app

查看:76
本文介绍了比较两次API调用的结果,并在MEAN应用中返回它们的差值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:由于找不到正确的解决方案,因此我更改了 应用程序的结构有点,并发布了另一个问题: 猫鼬-查找不在列表中的文档

EDIT: Since I wasn't able to find a correct solution, I changed the application's structure a bit and posted another question: Mongoose - find documents not in a list

我有一个带有以下三个模型的MEAN应用程序:UserTask,用于跟踪我拥有UserTask的用户分配给哪个任务的情况,如下所示:

I have a MEAN app with three models: User, Task, and for keeping track of which task is assigned to which user I have UserTask, which looks like this:

const mongoose = require("mongoose");
const autopopulate = require("mongoose-autopopulate");

const UserTaskSchema = mongoose.Schema({
  completed: { type: Boolean, default: false },
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    autopopulate: true
  },
  taskId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Task",
    autopopulate: true
  }      
});
UserTaskSchema.plugin(autopopulate);

module.exports = mongoose.model("UserTask", UserTaskSchema);

在我的前端应用程序中,我具有AngularJS服务,并且已经具有用于获取所有用户,所有任务以及分配给特定用户的任务的功能(通过使用给定的userId获取所有UserTasks.例如:

In my frontend app I have AngularJS services and I already have functions for getting all users, all tasks, and tasks which are assigned to a particular user (by getting all UserTasks with given userId. For example:

// user-task.service.js
function getAllUserTasksForUser(userId) {
  return $http
    .get("http://localhost:3333/userTasks/byUserId/" + userId)
    .then(function(response) {
      return response.data;
    });
}

// task-service.js
function getAllTasks() {
  return $http.get("http://localhost:3333/tasks").then(function(response) {
    return response.data;
  });
}

然后我在控制器中使用此数据,如下所示:

Then I'm using this data in my controllers like this:

userTaskService
    .getAllUserTasksForUser($routeParams.id)
    .then(data => (vm.userTasks = data));

...并且由于使用autopopulate插件,我在UserTasks中拥有完整的UserTask对象.到目前为止,一切都很好.

...and because of autopopulate plugin I have complete User and Task objects inside the UserTasks that I get. So far, so good.

现在,我需要获取未分配给特定User的所有Task.我想我应该首先获取所有Task,然后获取给定userId的所有UserTasks,然后使用某种不包含在其中"的过滤器进行某种区别.

Now I need to get all Tasks which are not assigned to a particular User. I guess I should first get all Tasks, then all UserTasks for a given userId, and then make some kind of difference, with some "where-not-in" kind of filter.

我仍然是所有MEAN组件的新手,我对所有这些then()和Promise之类的东西并不熟悉...我真的不确定如何做到这一点.我尝试使用多个then(),但没有成功.谁能给我提示吗?

I'm still a newbie for all the MEAN components, I'm not familiar with all those then()s and promises and stuff... and I'm really not sure how to do this. I tried using multiple then()s but with no success. Can anyone give me a hint?

推荐答案

您可以在服务器/API端执行,这样会更有效率.

You can do at server/API side that will more efficient.

如果要这样做,请在客户端尝试

In client side, if you want to do then try below

var userid = $routeParams.id;
userTaskService
    .getAllTasks()
    .then((data) => {
        vm.userTasks = data.filter(task => task.userId !== userid)
    });

这篇关于比较两次API调用的结果,并在MEAN应用中返回它们的差值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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