繁体   English   中英

如何在节点js中过滤用户ID

[英]how to filter user Id in node js

我已经使用node.js创建了2个API,在后端我正在使用mongodb。 我的第一个API我拥有所有的用户ID列表,而另一个API我将获得单个用户ID,其中我需要使用包含用户ID列表的第一个API进行过滤,如果用户ID匹配,那么我需要发送受关注的ID。

架构图

var mongoose = require('mongoose');

var filtertripSchema = new mongoose.Schema({
    filterId : String
});

在此API中,我将收到一个用户ID

module.exports = mongoose.model('filtertrip', filtertripSchema);
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const filter = require('../models/filtertrip');
const expensemaster = require('../models/expensemaster.js'); //here i am call another API which contain list of userid

  router.post('/',  function(req, res, next) {
    filter.create(req.body, function (err, products) {
      if (err) return next(err);
     var Id = products.filterId;
    });
  });

包含用户标识列表的架构

var expensemaster = new mongoose.Schema({
    // expensecategoryname: String,
    expensecategory : {type: mongoose.Schema.Types.ObjectId, ref: 'expensecategory'},
    tripid : {type: mongoose.Schema.Types.ObjectId, ref: 'tripmaster'}
});

输出量

{
        "_id": "5aba44a56bef1ea0e8817d1e",
        "expensecategory": "5ab89f17f03cfe71f065eda7"
        "tripid": {
            "usersid": [
                "5ab9e0f2a9bcd1050c2f1c92",
                "5ab9e134a9bcd1050c2f1c93",
                "5ab9e169a9bcd1050c2f1c94",
                "5ab9e1a3a9bcd1050c2f1c95",
                "5ab9e2f6a9bcd1050c2f1c97",
                "5aba037057a5b166800c13dd"
            ],
            "_id": "5aba3c9b6bef1ea0e8817d05"
        },
        "__v": 0
    },
    {
        "_id": "5aba44a56bef1ea0e8817d22", // I NEED TO SEND THIS ID IF THE USERID IS MATCHING
        "expensecategory": "5ab89f17f03cfe71f065eda7",
        "tripid": {
            "usersid": [
                "5ab9e0f2a9bcd1050c2f1c92",
                "5ab9e134a9bcd1050c2f1c93",
                "5ab9e169a9bcd1050c2f1c94",
                "5ab9e1a3a9bcd1050c2f1c95",
                "5ab9e2f6a9bcd1050c2f1c97",
                "5aba037057a5b166800c13dd"
            ],
            "_id": "5aba3c9b6bef1ea0e8817d05"
        },
        "__v": 0
    }

当我从另一个Api获取用户ID时,我需要调用它并进行过滤并发送_id

请帮助我,我是node.js的新手

您应该使用middlewares 在用于获取userId的第一个API中,将控制权传递给另一个middleware ,该middleware将过滤id并最终发送回响应。

在以下网址了解有关快递中间件的更多信息: https : //expressjs.com/en/guide/using-middleware.html

暂无
暂无

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

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