如何从nodejs中的get选项获取集合名称
How to get collection name from get option in nodejs
我正在尝试从 mongodb 获取 table 数据取决于传递集合 name.In 我的 angular 项目 我有一个服务。我通过 get 方法传递集合名称,但我不知道如何在 gettable 数据方法中获取该集合名称。
product.component.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 //Node.js
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)
})
}
我正在尝试从 mongodb 获取 table 数据取决于传递集合 name.In 我的 angular 项目 我有一个服务。我通过 get 方法传递集合名称,但我不知道如何在 gettable 数据方法中获取该集合名称。
product.component.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 //Node.js
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)
})
}