【问题标题】:How to create a stitch Service with query params如何使用查询参数创建缝合服务
【发布时间】:2019-06-21 02:27:48
【问题描述】:

我正在使用 MongoDB Stitch 创建一个数据 API,我想知道如何使用查询参数创建一个函数(当我调用我的 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 ;
};

【问题讨论】:

    标签: mongodb-stitch


    【解决方案1】:

    这是一个极简示例这样的功能:

    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 外部测试(例如在 Postman 中)

    使用 Stitch 生成的Webhook URL,然后追加查询参数。

    https://webhooks.mongodb-stitch.com/api/client/v2.0/app/MY_STITCH_APP_ID/service/http/incoming_webhook/MY_WEBHOOK_NAME?coll=myCollection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多