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"
};
}
};
我正在使用httpTrigger编写Azure函数。触发后,应将其保存到azure存储模拟器中托管的队列名称“ azurequeue”。
我收到
的错误
我在做错什么吗?如何解决这个问题。为什么会这样?
答案 0 :(得分:1)
好像您将HttpTrigger和QueueTrigger混合在一起。
如果您想使用上面的逻辑将消息写入队列,请确保您的function.json
如下所示。
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
如果首选由Azure函数提供的outputbinding功能,请使用下面的代码。不必担心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;
}
};
别忘了在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"
}
]
}
为避免将来遇到其他障碍,请查看Azure Function guidance of javascript。