我正在用MongoDB创建一个Data,我想知道如何使用查询参数创建函数(在调用API时必须给出参数),我想在调用API时给出集合名。
这是我的职责
exports = function(arg) {
const mongodb = context.services.get("mongodb-atlas");
const mycollection = mongodb.db("STM32").collection(arg);
console.log(arg) ;
var array = mycollection.find({}).toArray();
return array ;
};发布于 2019-06-20 18:27:48
下面是这样一个函数的极简主义示例:
exports = function(payload, response) {
// retrieve collection name from querystring (e.g. ?coll=myCollection)
const {coll} = payload.query;
// log collection name to confirm receipt from querystring
console.log("collection name passed: ", coll);
// query a mongodb service, passing the parameterized collection name
const doc = context.services.get("mongodb-atlas").db("mydb").collection(coll).find().toArray();
return doc;
};函数编辑器的测试:
exports({query: {coll: 'myCollection'}})外部测试(例如在邮递员中)
使用Stitch生成的Webhook URL,然后追加查询参数。
https://stackoverflow.com/questions/56025823
复制相似问题