简体   繁体   中英

MongoDB Atlas Trigger — Receiving a “StitchError: HTTP request failed: unexpected status code: expected=200, actual=415” error

We're trying to create a trigger in MongoDB Atlas that will automatically reduce our Azure tier of an evening to save cost. The function we've put together (probably incorrectly!) returns an error when we try to run it. The result output is:

logs: 
uncaught promise rejection: StitchError: HTTP request failed: unexpected status code: expected=200, actual=415
> result: 
{
  "$undefined": true
}
> result (JavaScript): 
EJSON.parse('{"$undefined":true}')

We've tried messing around with the headers and body, but the end result is always the same.

Here's the WIP function:

exports = function() {

  const response = context.http.patch({
    scheme: "https",
    host: "cloud.mongodb.com",
    path: "/api/atlas/v1.0/groups/abc123/clusters/fake-server",
    username: "usernamehere",
    password: "passwordhere",
    digestAuth: true,
    headers: {"Content-Type": [ "application/json" ]},
    body: {"clusterType":"REPLICASET", "providerSettings":{"providerName":"AZURE", "instanceSizeName":"M10", "regionName":"regionhere" } },
    encodeBodyAsJSON: true
  });
};

Any help would be greatly appreciated.

It turns out that we had it right the whole time.

Executing the exact same code as above is now correctly reducing our tier. I don't know what was causing the MongoDB Atlas API to think that our headers and / or request body were incorrect, but it's perfectly happy with it today.

The only other thing I will point out is that if you want this part of the error to go away...

> result: 
{
  "$undefined": true
}
> result (JavaScript): 
EJSON.parse('{"$undefined":true}')

...you need to change the function to async / await like so:

exports = async function() {

  const response = await context.http.patch({ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM