简体   繁体   English

Azure队列存储消息未保存到正确的队列中

[英]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. 我正在用httpTrigger写azure函数。 When triggered is should save to a queue name 'azurequeue' hosted in azure storage emulator. 触发后,应将其保存到azure存储模拟器中托管的队列名称“ azurequeue”。

error i am getting 我得到的错误 错误

存储

Is there something i am doing wrong? 我做错什么了吗? how to solve this. 如何解决这个问题。 why is this happening? 为什么会这样呢?

Looks like you mix HttpTrigger and QueueTrigger together. 好像您将HttpTrigger和QueueTrigger混合在一起。

If you want to use your own logic above to write message to queue, make sure your function.json is like below. 如果要使用上面的逻辑将消息写入队列,请确保您的function.json如下所示。

{
  "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. 如果首选由Azure函数提供的outputbinding功能,请使用下面的代码。 No need to worry that azurequeue doesn't exist, output binding internally uses createQueueIfNotExists. 无需担心azurequeue不存在,输出绑定内部使用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 . 并且不要忘记在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 . 为避免将来遇到其他障碍,请查看javascript的Azure函数指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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