如何从nodejs中的get选项获取集合名称

编程入门 行业动态 更新时间:2024-10-08 04:28:58

如何从nodejs中的get<a href=https://www.elefans.com/category/jswz/34/1771211.html style=选项获取集合名称"/>

如何从nodejs中的get选项获取集合名称

我试图从mongodb获取表数据取决于传递的集合名称。在我的有角项目中,我有一份服务。我正在通过get方法传递集合名称,但我不知道如何在gettable数据方法中获取该集合名称。

productponent.js:

    ngOnInit(){ 
     this.getProductsData('table'); 
    }

    getProductsData(collection){ 
     this.userService.getTableData(collection).subscribe(
      res => {
        this.tableData =  res; 
      },
      err => {
        console.log(err);

      }
    );

  }

user.service.ts:

getTableData(collection){ 
return this.http.get('http://localhost:3000/api/getTableData',collection);
}

table.controller.js // Nodejs

    const mongoose = require('mongoose');
const passport = require('passport');
const _ = require('lodash');

var userSchemaTable = new mongoose.Schema({
    product_name: {
        type: String
    },
    price: {
        type: String
    },
    catogery: {
        type: String
    }
}, { collection: 'table' });


mongoose.model('table', userSchemaTable);

const Table = mongoose.model('table');

module.exports.getTableData = (req, res, next) => {

    console.log(collection)

    How to get collection name here??????????????????????

  collection.find({}, function(err, docs) {
        if (err) {
            console.log('ss' + err);
            return
        }
        return res.json(docs)
    }) 
}
回答如下:

将您的getTableData方法更改为喜欢的方法,改为传递object

getTableData(collection){ 
return this.http.get(`http://localhost:3000/api/getTableData?collection=${collection}`);
}

然后像这样在这里获取集合名称

module.exports.getTableData = (req, res, next) => {

 let collection=req.query.collection;
    console.log(collection)

  collection.find({}, function(err, docs) {
        if (err) {
            console.log('ss' + err);
            return
        }
        return res.json(docs)
    }) 
}

更多推荐

如何从nodejs中的get选项获取集合名称

本文发布于:2024-05-07 08:57:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   名称   nodejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!