无翼漫画

Building a REST API with MongoDB Stitch

Andrew Morgan

#Technical#Cloud#Stitch#JavaScript

One of the great things about MongoDB Stitch is that it often removes the need to build REST APIs to grant access to your data from frontend applications – simply use to make MongoDB queries from your frontend code. However, there are often cases where you need to open up some of your data to other applications which don't use the – fortunately, Stitch makes it incredibly easy to build REST APIs for these occasions.

无翼漫画I enjoy tracking my location by checking into Swarm/FourSquare, but I want to get some extra value from that data – that means getting it into a MongoDB collection.

FourSquare provides an that's triggered whenever you check in – by linking that to the to send an HTTP POST request, I can forward that check-in data to Stitch. This is where Stitch comes in; a simple Stitch HTTP service webhook receives that POST request and writes the data to MongoDB:

exports = function(payload) {
  var queryArg = payload.query.arg || '';
  var body = {};
  if (payload.body) {
  body = EJSON.parse(payload.body.text());
  }
  
  var owner_id = context.functions.execute("ownerFromEmail", body.email);
  var checkin = {
    owner_id: owner_id.owner_id,
    email: body.email,
    venueName: body.venue,
    date: body.checkinDate,
    url: body.url,
    locationImg: body.location + "&key=" + context.values.get("GoogleMapsStaticKey")
  };
  
  return context.functions.execute("checkin", checkin);
};

The webhook uses the checkin function:

exports = function(checkin){
    var atlas = context.services.get("mongodb-atlas");
    var checkinColl = atlas.db("trackme").collection("checkins");
    try {
      checkinColl.insertOne(checkin);
    } catch (e) {
      console.log("Error inserting checkin doc: " + e);
      return e.message();
    }
};

Note that when configuring the HTTP service, I set an API key that the requestor must include as a secret query parameter:

Secure Stitch webhook with secret

IFTTT FourSquare applet

无翼漫画Now that the data is in MongoDB, there's no limit to what I can do with it. For example, I want a dashboard for my check-in data, and one thing I want to include is a graph on my most frequent check-ins:

Foursquare check-in grapsh

To implement that, I write a new Stitch function:

exports = function(limit){
    var atlas = context.services.get("mongodb-atlas");
    var checkinColl = atlas.db("trackme").collection("checkins");
    var checkins = checkinColl.aggregate([
      {$match: {owner_id: context.user.id}},
      {$group: {
        _id: "$venueName",
        count: {$sum: 1}
      }},
      {$sort: {count: -1}},
      {$limit: limit},
      {$project: {
        venue: "$_id",
        _id: 0,
        count: 1
      }}
    ]).toArray();
    return checkins;
};

无翼漫画From the frontend application, it then takes a single method call to retrieve the data:

this.props.stitchClient
    .executeFunction('popularCheckins', 10)
    .then(
      checkinData => {
        this.setState({checkins: checkinData});
        this.createBarChart();
      },
      error => {
        console.log(
          "Failed to fetch most popular check-ins: "
          + error)
    })

You can recreate this Stitch and frontend app for your self by and .

无翼漫画Creating your first Stitch app? Start with one of the .

Want to learn more about MongoDB Stitch? Read the white paper.

秒速赛车平台【复制网址gd1144.com】 秒速赛车【gd1133.com光大彩票】-新闻 秒速赛车【gd1100.com光大彩票】-法治 秒速赛车【gd1144.com光大彩票】-案件 秒速赛车【gd1155.com光大彩票】-检察 秒速赛车【gd1166.com光大彩票】-理论 秒速赛车【gd1177.com光大彩票】-新媒体 秒速赛车【gd9900.com光大彩票】-视频 秒速赛车【gd9911.com光大彩票】-评论 秒速赛车【gd9922.com光大彩票】-文化 秒速赛车【gd9933.com光大彩票】-人物 秒速赛车【gd9944.com光大彩票】-舆情 秒速赛车【gd9955.com光大彩票】-装备 无翼漫画 秒速赛车【gd9977.com光大彩票】-今日推荐 秒速赛车【gd1133.com光大彩票】-图片聚焦 秒速赛车【gd1100.com光大彩票】-以案说法 秒速赛车【gd1144.com光大彩票】-检察视界 秒速赛车【gd1155.com光大彩票】-城东区 秒速赛车【gd1166.com光大彩票】-城中区 秒速赛车【gd1177.com光大彩票】-城西区 秒速赛车【gd9900.com光大彩票】-城北区 秒速赛车【gd9911.com光大彩票】-大通县 秒速赛车【gd9922.com光大彩票】-湟中县 秒速赛车【gd9933.com光大彩票】-湟源县 秒速赛车【gd9944.com光大彩票】-经济技术开发区 秒速赛车【gd9955.com光大彩票】-城南新区 秒速赛车【gd9977.com光大彩票】-高新技术开发区 秒速赛车【gd1133.com光大彩票】-海湖新区 秒速赛车【gd1100.com光大彩票】-无翼漫画 秒速赛车【gd1144.com光大彩票】-滚动 秒速赛车【gd1155.com光大彩票】-国内 秒速赛车【gd1166.com光大彩票】-国际 秒速赛车【gd1177.com光大彩票】-军事 秒速赛车【gd9900.com光大彩票】-社会 秒速赛车【gd9911.com光大彩票】-财经 秒速赛车【gd9922.com光大彩票】-产经 秒速赛车【gd9933.com光大彩票】-房产 秒速赛车【gd9944.com光大彩票】-金融 秒速赛车【gd9955.com光大彩票】-证券 秒速赛车【gd9977.com光大彩票】-汽车 秒速赛车【gd1133.com光大彩票】-IT 秒速赛车【gd1100.com光大彩票】-能源 秒速赛车【gd1144.com光大彩票】-港澳 秒速赛车【gd1155.com光大彩票】-台湾 秒速赛车【gd1166.com光大彩票】-华人 秒速赛车【gd1177.com光大彩票】-侨网 秒速赛车【gd9900.com光大彩票】-经纬 秒速赛车【gd9911.com光大彩票】-English 秒速赛车【gd9922.com光大彩票】-图片 秒速赛车【gd9933.com光大彩票】-视频 秒速赛车【gd9944.com光大彩票】-直播 北京PK10【gd9955.com光大彩票】-娱乐 PK10开奖【gd9977.com光大彩票】-体育 pk10平台【gd1133.com光大彩票】-文化 Pk10计划【gd1100.com光大彩票】-健康 pk10网址【gd1144.com光大彩票】-生活 pk10开户【gd1155.com光大彩票】-葡萄酒 pk10赛车【gd1166.com光大彩票】-微视界 PK10投注平台【gd1177.com光大彩票】-演出 PK10注册【gd9900.com光大彩票】-专题 PK10开户【gd9911.com光大彩票】-理论 北京PK10【gd9922.com光大彩票】-新媒体 PK10开奖【gd9933.com光大彩票】-供稿 pk10平台【gd9944.com光大彩票】-新闻 Pk10计划【gd9955.com光大彩票】-时事 pk10网址【gd9977.com光大彩票】-视频 pk10开户【gd1133.com光大彩票】-评论 pk10赛车【gd1100.com光大彩票】-股票 PK10投注平台【gd1144.com光大彩票】-区块链 PK10注册【gd1155.com光大彩票】-港股 PK10开户【gd1166.com光大彩票】-科创 北京PK10【gd1177.com光大彩票】-基金 PK10开奖【gd9900.com光大彩票】-私募 pk10平台【gd9911.com光大彩票】-理财 Pk10计划【gd9922.com光大彩票】-信托 pk10网址【gd9933.com光大彩票】-P2P pk10开户【gd9944.com光大彩票】-科技 pk10赛车【gd9955.com光大彩票】-债券 PK10投注平台【gd9977.com光大彩票】-互金 PK10注册【gd1133.com光大彩票】-期货 PK10开户【gd1100.com光大彩票】-原油 北京PK10【gd1144.com光大彩票】-大宗 PK10开奖【gd1155.com光大彩票】-期指 pk10平台【gd1166.com光大彩票】-保险 Pk10计划【gd1177.com光大彩票】-银行 pk10网址【gd9900.com光大彩票】-黄金 pk10开户【gd9911.com光大彩票】-外汇 pk10赛车【gd9922.com光大彩票】-行情 PK10投注平台【gd9933.com光大彩票】-数据 PK10注册【gd9944.com光大彩票】-房产 PK10开户【gd9955.com光大彩票】-汽车 北京PK10【gd9977.com光大彩票】-博客 PK10开奖【gd1133.com光大彩票】-农金 pk10平台【gd1100.com光大彩票】-名家 Pk10计划【gd1144.com光大彩票】-消金 pk10网址【gd1155.com光大彩票】-美股 pk10开户【gd1166.com光大彩票】-创投 pk10赛车【gd1177.com光大彩票】-游戏 PK10投注平台【gd9900.com光大彩票】-滚动 PK10注册【gd9911.com光大彩票】-新浪 PK10开户【gd9922.com光大彩票】-正义网