Node-slack-sdk: NodeError: The "last argument" argument must be of type function

Created on 15 Apr 2018  路  4Comments  路  Source: slackapi/node-slack-sdk

Hi, I'm stuck with this problem while trying to send a message after an event call using the API.

const SlackClient = require('@slack/client').WebClient;
const createSlackEventAdapter = require('@slack/events-api').createSlackEventAdapter;

// *** Initialize event adapter using verification token from environment variables ***
const slackEvents = createSlackEventAdapter(process.env.SLACK_VERIFICATION_TOKEN, {
  includeBody: true
});```



// *** Greeting any user that says "hi" ***
slackEvents.on('message', (message, body) => {
  // Only deal with messages that have no subtype (plain messages) and contain 'hi'
  if (!message.subtype && message.text.indexOf('hi') >= 0) {
    // Initialize a client
    const slack = getClientByTeamId(body.team_id);
    // Handle initialization failure
    if (!slack) {
      return console.error('No authorization found for this team. Did you install this app again after restarting?');
    }
    // Respond to the message back in the same channel
    slack.chat.postMessage(message.channel, `Hello <@${message.user}>! :tada:`)
      .catch(console.error);
  }
});

When I try to call the postMessage function Node crashes and i get this:

NodeError: The "last argument" argument must be of type function

I'll really appreciate some hints.

question

All 4 comments

@FrancescoPolitano thanks for bringing this to our attention. it looks like you are using the example from the @slack/events-api documentation, which has not been updated to use v4 of this package. i'll make a note of this on the issue tracker for that package.

in the meantime, the fix is to change the postMessage line to the following:

// Respond to the message back in the same channel
slack.chat.postMessage({ channel: message.channel, text: `Hello <@${message.user}>! :tada:` })
  .catch(console.error);

Thank you! I have another problem, postmessage give me an error for circular structures on WebClient.ts line 528, but I don't know how to resolve circular dependency on your "value" structure
...
if (Buffer.isBuffer(value) || isStream(value)) { containsBinaryData = true; } else if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') { // if value is anything other than string, number, boolean, binary data, a Stream, or a Buffer, then encode it // as a JSON string. serializedValue = JSON.stringify(value); }
...

@FrancescoPolitano the value that line is referring to is part of the options that you have passed in, so it seems like you need to look closer at what you've passed in.

for example, an options parameter like the following would cause the same kind of error:

const attachment = {
  text: 'My attachment',
  fields: [
    {
      title: 'Something',
      value: attachment,  // ERROR: this is a circular reference, JSON.stringify() will fail
    },
  ],
};
web.chat.postMessage({
  channel: 'C12345',
  text: `Hello, world',
  attachments: [attachment],
}).then(console.error);

There's no good way to turn a circular reference into a JSON object to send to Slack. It's likely a bug in your code. Take a closer look and try to flatten the structure as much as possible.

Thank you for your help and for your good code.

Was this page helpful?
0 / 5 - 0 ratings