admin管理员组文章数量:814911
如何从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选项获取集合名称
版权声明:本文标题:如何从nodejs中的get选项获取集合名称 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1715131254a821379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论