最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin20浏览0评论

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

如何从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)
    }) 
}
发布评论

评论列表(0)

  1. 暂无评论