gpt4 book ai didi

javascript - 返回时 Microsoft.Azure.WebJobs.Script : Unable to cast object of type 'System.String' to type 'Microsoft.AspNetCore.Http.HttpRequest' .

转载 作者:行者123 更新时间:2023-12-03 02:34:30 24 4
gpt4 key购买 nike

我有一个用 JS 编写的 Azure 函数,它由服务总线触发并生成文件到 Blob 存储。当我尝试返回 HTTP 结果时,我收到如下错误:

System.Private.CoreLib:执行函数时出现异常:Functions.categoryMessageConsumer。 Microsoft.Azure.WebJobs.Script:无法将“System.String”类型的对象转换为“Microsoft.AspNetCore.Http.HttpRequest”类型。

我不知道为什么结果试图映射到 HttpRequest 对象。

索引.ts:

import { AzureFunction, Context, HttpRequest } from '@azure/functions';
...

const serviceBusTopicTrigger: AzureFunction = async function(context: Context, req: HttpRequest) {
let categoryMessage: CategoryMessage = Object.assign(new CategoryMessage(), req);
let messageValidationResult = await categoryMessage.validate();
if(!messageValidationResult.isValid) {
context.log.error(messageValidationResult.errors);
return {
status: 400,
body: "Unexpected error"
};
}

...
}

function.json 输出绑定(bind):

...
{
"type": "http",
"direction": "out",
"name": "$return"
}
...

主机.json

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}

最佳答案

var http = require('https');
module.exports = function (context, eventHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${eventHubMessages}`);

eventHubMessages.forEach((message, index) => {

function callService(msg) {
let promise = new Promise((resolve, reject) => {
var options = {
host: process.env["HOST"],
port: process.env["PORT"],
path: process.env["PATH"],
method: process.env["WEBSERVICE_METHOD"],
headers: {
'Content-Type': process.env["WEBSERVICE_CONTENT_TYPE"],
'x-api-key' : process.env["WEBSERVICE_API_KEY"]
}
};
var response = '';
const request = http.request(options, (res) => {
res.on('data', (d) => {
response += d;
})

res.on('end', (d) => {
context.res = {
body: response
};
resolve(response);
})
});

request.on('error', (error) => {
context.log.error(error);
reject(error);
context.done();
})

request.write(msg);
request.end();

});
promise.then((response) => {
context.log(`mensagge send: ${msg}`);
context.log(`response: ${response}`);
context.done();
});

}

callService(message);
});
};

这是我的index.js

现在它是我的 function.js:

{
"bindings": [
{
"type": "eventHubTrigger",
"name": "eventHubMessages",
"direction": "in",
"eventHubName": "event-hub-name",
"connection": "CONNECTION_EVENT_HUB",
"cardinality": "many",
"consumerGroup": "$Default",
"dataType": "string"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}

在 local.settings.json 中,我声明了所有环境变量,例如 HOST、PORT、CONNECTION_EVENT_HUB 等。

关于javascript - 返回时 Microsoft.Azure.WebJobs.Script : Unable to cast object of type 'System.String' to type 'Microsoft.AspNetCore.Http.HttpRequest' .,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63724534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com