Help with event-triggerd function

Hi all - I’m having trouble getting my event-triggered function to work, it doesn’t seem to be firing at all.

description

I want to trigger a branch deploy when a form submission is created.

The netlify function doesn’t seem to be firing after form submission.

However, when I manually invoke the function file from my local dev environment, the build trigger works.

submission-created.js content

Here’s the contents of my submission-created.js function file:

'use strict';

const axios = require('axios').default;
require('dotenv').config();

const buildHook = `https://api.netlify.com/build_hooks/${process.env.HOOK_ID}`;
const branch = 'render-comments';
const title = 'triggered+by+submission-created+build+hook';
const params = `?trigger_branch=${branch}&trigger_title=${title}`;

exports.handler = function(event, context, callback) {
  axios
    .post(`${buildHook}${params}`, {})
    .then(function(response) {
      console.log(response);
      callback(null, {
        statusCode: 201,
        body: 'Comment submitted!'
      });
    })
    .catch(function(error) {
      console.log(error);
    });
};

If it’s useful to know - i’m running all this on a development branch, not on the master branch which is deployed in production.

Thanks for any tips!

Actually, after further investigating my netlify site dashboard, I found the log for the function on my dev branch. Here is the error message from the log:

(it seems like an axios error, but not sure why that would be the case since the axios call works for me locally)

9:24:56 AM: 2020-02-16T14:24:56.578Z	c11ec822-0321-47db-adc7-fec51e612690	INFO	Error: Request failed with status code 404    at createError (/var/task/node_modules/axios/lib/core/createError.js:16:15)    at settle (/var/task/node_modules/axios/lib/core/settle.js:17:12)    at IncomingMessage.handleStreamEnd (/var/task/node_modules/axios/lib/adapters/http.js:237:11)    at IncomingMessage.emit (events.js:228:7)    at endReadableNT (_stream_readable.js:1185:12)    at processTicksAndRejections (internal/process/task_queues.js:81:21) {  config: {    url: 'https://api.netlify.com/build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook',    method: 'post',    data: '{}',    headers: {      Accept: 'application/json, text/plain, */*',      'Content-Type': 'application/json;charset=utf-8',      'User-Agent': 'axios/0.19.0',      'Content-Length': 2    },    transformRequest: [ [Function: transformRequest] ],    transformResponse: [ [Function: transformResponse] ],    timeout: 0,    adapter: [Function: httpAdapter],    xsrfCookieName: 'XSRF-TOKEN',    xsrfHeaderName: 'X-XSRF-TOKEN',    maxContentLength: -1,    validateStatus: [Function: validateStatus]  },  request: ClientRequest {    _events: [Object: null prototype] {      socket: [Function],      abort: [Function],      aborted: [Function],      error: [Function],      timeout: [Function],      prefinish: [Function: requestOnPrefinish]    },    _eventsCount: 6,    _maxListeners: undefined,    outputData: [],    outputSize: 0,    writable: true,    _last: true,    chunkedEncoding: false,    shouldKeepAlive: false,    useChunkedEncodingByDefault: true,    sendDate: false,    _removedConnection: false,    _removedContLen: false,    _removedTE: false,    _contentLength: null,    _hasBody: true,    _trailer: '',    finished: true,    _headerSent: true,    socket: TLSSocket {      _tlsOptions: [Object],      _secureEstablished: true,      _securePending: false,      _newSessionPending: false,      _controlReleased: true,      _SNICallback: null,      servername: 'api.netlify.com',      alpnProtocol: false,      authorized: true,      authorizationError: null,      encrypted: true,      _events: [Object: null prototype],      _eventsCount: 9,      connecting: false,      _hadError: false,      _parent: null,      _host: 'api.netlify.com',      _readableState: [ReadableState],      readable: true,      _maxListeners: undefined,      _writableState: [WritableState],      writable: false,      allowHalfOpen: false,      _sockname: null,      _pendingData: null,      _pendingEncoding: '',      server: undefined,      _server: null,      ssl: [TLSWrap],      _requestCert: true,      _rejectUnauthorized: true,      parser: null,      _httpMessage: [Circular],      [Symbol(res)]: [TLSWrap],      [Symbol(asyncId)]: 5,      [Symbol(kHandle)]: [TLSWrap],      [Symbol(lastWriteQueueSize)]: 0,      [Symbol(timeout)]: null,      [Symbol(kBuffer)]: null,      [Symbol(kBufferCb)]: null,      [Symbol(kBufferGen)]: null,      [Symbol(kBytesRead)]: 0,      [Symbol(kBytesWritten)]: 0,      [Symbol(connect-options)]: [Object]    },    connection: TLSSocket {      _tlsOptions: [Object],      _secureEstablished: true,      _securePending: false,      _newSessionPending: false,      _controlReleased: true,      _SNICallback: null,      servername: 'api.netlify.com',      alpnProtocol: false,      authorized: true,      authorizationError: null,      encrypted: true,      _events: [Object: null prototype],      _eventsCount: 9,      connecting: false,      _hadError: false,      _parent: null,      _host: 'api.netlify.com',      _readableState: [ReadableState],      readable: true,      _maxListeners: undefined,      _writableState: [WritableState],      writable: false,      allowHalfOpen: false,      _sockname: null,      _pendingData: null,      _pendingEncoding: '',      server: undefined,      _server: null,      ssl: [TLSWrap],      _requestCert: true,      _rejectUnauthorized: true,      parser: null,      _httpMessage: [Circular],      [Symbol(res)]: [TLSWrap],      [Symbol(asyncId)]: 5,      [Symbol(kHandle)]: [TLSWrap],      [Symbol(lastWriteQueueSize)]: 0,      [Symbol(timeout)]: null,      [Symbol(kBuffer)]: null,      [Symbol(kBufferCb)]: null,      [Symbol(kBufferGen)]: null,      [Symbol(kBytesRead)]: 0,      [Symbol(kBytesWritten)]: 0,      [Symbol(connect-options)]: [Object]    },    _header: 'POST /build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook HTTP/1.1\r\n' +      'Accept: application/json, text/plain, */*\r\n' +      'Content-Type: application/json;charset=utf-8\r\n' +      'User-Agent: axios/0.19.0\r\n' +      'Content-Length: 2\r\n' +      'Host: api.netlify.com\r\n' +      'Connection: close\r\n' +      '\r\n',    _onPendingData: [Function: noopPendingOutput],    agent: Agent {      _events: [Object: null prototype],      _eventsCount: 1,      _maxListeners: undefined,      defaultPort: 443,      protocol: 'https:',      options: [Object],      requests: {},      sockets: [Object],      freeSockets: {},      keepAliveMsecs: 1000,      keepAlive: false,      maxSockets: Infinity,      maxFreeSockets: 256,      maxCachedSessions: 100,      _sessionCache: [Object]    },    socketPath: undefined,    method: 'POST',    path: '/build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook',    _ended: true,    res: IncomingMessage {      _readableState: [ReadableState],      readable: false,      _events: [Object: null prototype],      _eventsCount: 3,      _maxListeners: undefined,      socket: [TLSSocket],      connection: [TLSSocket],      httpVersionMajor: 1,      httpVersionMinor: 1,      httpVersion: '1.1',      complete: true,      headers: [Object],      rawHeaders: [Array],      trailers: {},      rawTrailers: [],      aborted: false,      upgrade: false,      url: '',      method: null,      statusCode: 404,      statusMessage: 'Not Found',      client: [TLSSocket],      _consuming: false,      _dumped: false,      req: [Circular],      responseUrl: 'https://api.netlify.com/build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook',      redirects: []    },    aborted: false,    timeoutCb: null,    upgradeOrConnect: false,    parser: null,    maxHeadersCount: null,    _redirectable: Writable {      _writableState: [WritableState],      writable: true,      _events: [Object: null prototype],      _eventsCount: 2,      _maxListeners: undefined,      _options: [Object],      _redirectCount: 0,      _redirects: [],      _requestBodyLength: 2,      _requestBodyBuffers: [],      _onNativeResponse: [Function],      _currentRequest: [Circular],      _currentUrl: 'https://api.netlify.com/build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook'    },    [Symbol(kNeedDrain)]: false,    [Symbol(isCorked)]: false,    [Symbol(kOutHeaders)]: [Object: null prototype] {      accept: [Array],      'content-type': [Array],      'user-agent': [Array],      'content-length': [Array],      host: [Array]    }  },  response: {    status: 404,    statusText: 'Not Found',    headers: {      'cache-control': 'no-cache',      'content-type': 'text/plain; charset=utf-8',      date: 'Sun, 16 Feb 2020 14:24:56 GMT',      'strict-transport-security': 'max-age=31536000',      vary: 'Origin',      'x-content-type-options': 'nosniff',      'x-frame-options': 'ALLOWALL',      'x-nf-srv-version': 'b16237a0ff2b3bf7915250067f95ce0806b166fa',      'x-request-id': 'e6bc1bad-e9b7-4ec7-b094-f0f07230682a',      'x-runtime': '0.008714',      'content-length': '14',      via: '1.1 google',      'alt-svc': 'clear',      connection: 'close'    },    config: {      url: 'https://api.netlify.com/build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook',      method: 'post',      data: '{}',      headers: [Object],      transformRequest: [Array],      transformResponse: [Array],      timeout: 0,      adapter: [Function: httpAdapter],      xsrfCookieName: 'XSRF-TOKEN',      xsrfHeaderName: 'X-XSRF-TOKEN',      maxContentLength: -1,      validateStatus: [Function: validateStatus]    },    request: ClientRequest {      _events: [Object: null prototype],      _eventsCount: 6,      _maxListeners: undefined,      outputData: [],      outputSize: 0,      writable: true,      _last: true,      chunkedEncoding: false,      shouldKeepAlive: false,      useChunkedEncodingByDefault: true,      sendDate: false,      _removedConnection: false,      _removedContLen: false,      _removedTE: false,      _contentLength: null,      _hasBody: true,      _trailer: '',      finished: true,      _headerSent: true,      socket: [TLSSocket],      connection: [TLSSocket],      _header: 'POST /build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook HTTP/1.1\r\n' +        'Accept: application/json, text/plain, */*\r\n' +        'Content-Type: application/json;charset=utf-8\r\n' +        'User-Agent: axios/0.19.0\r\n' +        'Content-Length: 2\r\n' +        'Host: api.netlify.com\r\n' +        'Connection: close\r\n' +        '\r\n',      _onPendingData: [Function: noopPendingOutput],      agent: [Agent],      socketPath: undefined,      method: 'POST',      path: '/build_hooks/5e49370b645e93bc77b1e743?trigger_branch=render-comments&trigger_title=triggered+by+submission-created+build+hook',      _ended: true,      res: [IncomingMessage],      aborted: false,      timeoutCb: null,      upgradeOrConnect: false,      parser: null,      maxHeadersCount: null,      _redirectable: [Writable],      [Symbol(kNeedDrain)]: false,      [Symbol(isCorked)]: false,      [Symbol(kOutHeaders)]: [Object: null prototype]    },    data: 'Page not found'  },  isAxiosError: true,  toJSON: [Function]}
9:24:56 AM: Duration: 105.00 ms	Memory Usage: 80 MB	Init Duration: 155.58 ms	

It looks like the error is “Page not found”. I suspect that your env vars aren’t accessible. Are you defining these env vars in a netlify.toml file? If so, then you may have run into a bug. That said, can you try setting your env vars in your site’s dashboard instead and see if that helps?