title: If data is undefined, at least one attribute must be present pubsub
body: <p>I'm trying to use Google Cloud's Pub/Sub API to send messages to topic subscribers, but I'm having a problem where it's returning the message &quot;<strong>If data is undefined, at least one attribute must be present.</strong>&quot;. I'm going to use it with Cloud Functions. I have the following code which is the same as the official Pub/Sub NodeJS documentation with minor changes:</p>
  <pre><code>exports.helloWorld = async (req, res) =&gt; {
  
      const parse = require('url-parse');
      const punycode = require('punycode');
      const {PubSub} = require('@google-cloud/pubsub');
  
      let message = req.query.message || req.body.message || 'Hello World!';
  
      let referHeader = req.headers.referer;
      let userIP = req.socket.remoteAddress;
      let originHeader = req.headers.origin;
      let userAgenteHeader = req.useragent;
      let a = req.query.a;
      let s = req.query.s;
      let e = req.query.e;
  
      try {
          
          const pubSubClient = new PubSub({
              project_id: 'my-project',
              credentials: {
                  private_key: &quot;my_private_key&quot;,
                  client_email: 'email_project@myemail.com',
              }
          });
  
          a = 123; //Change for test
              
          let topicNameOrId = 'MY_TOPIC_ID';
  
          const data = JSON.stringify({
              &quot;data&quot;: &quot;analytics&quot;,
              &quot;attributes&quot;: {
                &quot;a&quot;: a
              }
            });
  
          const dataBuffer = Buffer.from(data);
  
          const messageId = await pubSubClient
            .topic(topicNameOrId)
            .publishMessage(dataBuffer);
  
          res.status(200).send(`Message ${messageId} published.`);
  
        } catch (error) {
  
          console.error();
          res.status(403).send(`${error.message}`);
  
          process.exitCode = 1;
        }
  };
  </code></pre>
  <p>I've tried to do it with JSON.stringify, without it, directly in the function, but nothing worked, it always gives the same error and I'm passing the data parameter normally.</p>