Search code examples
azureazure-storageazure-functions

Azure Queue Storage message not getting saved in the right queue On Azure Function


const azure = require('azure-storage');
const queueService = azure.createQueueService("UseDevelopmentStorage=true");

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    context.log('Request Headers = ' + JSON.stringify(req.headers));

    queueName = 'azurequeue';

    if ((req.query.parameter && req.query.func) || (req.body && req.body.parameter && req.body.func)) {

        var send = new Object();

        var message = 'Hello world!' 

        queueService.createQueueIfNotExists(queueName,function(err, result, response) {
            if (err) {
                context.log('here')
                context.error(err);
              return;
            }

            if (result.created) {
                context.log(`[Queue - Sender] Queue ${queueName} did not exist, created it`);
            }

            queueService.createMessage(queueName, message , (err, result, res) => {
                if (err) {
                    context.error(`[Queue - Sender] An error occurred: ${JSON.stringify(err)}`);
                }

                context.log(`[Queue - Sender] Sent: ${JSON.stringify(message)}`);
            });

          });

        send.ans = 'Hello world!'; 

        context.res = {
            body: send
        };

    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

i am writing an azure Function with httpTrigger. When triggered is should save to a queue name 'azurequeue' hosted in azure storage emulator.

error i am getting Error

Storage

Is there something i am doing wrong? how to solve this. why is this happening?


Solution

  • Looks like you mix HttpTrigger and QueueTrigger together.

    If you want to use your own logic above to write message to queue, make sure your function.json is like below.

    {
      "disabled": false,
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ]
    }
    

    If outputbinding feature provided by Azure function is preferred, let's use code below. No need to worry that azurequeue doesn't exist, output binding internally uses createQueueIfNotExists.

    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
        context.log('Request Headers = ' + JSON.stringify(req.headers));
    
        if ((req.query.parameter && req.query.func) || (req.body && req.body.parameter && req.body.func)) {
    
            var message = 'Hello world!' 
            var send = {'ans' : message};
    
            return {
                res: {
                    body: send
                },
                queueOutput: message
            };
        }
        else {
            context.res = {
                status: 400,
                body: "Please pass a name on the query string or in the request body"
            };
            return;
        }
    };
    

    And don't forget to add queue output in function.json.

    {
      "disabled": false,
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        },
        {
          "type": "queue",
          "direction": "out",
          "name":"queueOutput" ,
          "queueName": "azurequeue",
          "connection":"AzureWebJobsStorage"
        }
      ]
    }
    

    To avoid other obstacle in the future, have a look at Azure Function guidance of javascript.