[英]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
});
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.