我在做什么错?为什么功能超时?
我有一个Blob触发功能,可以正常工作:
const Jimp = require("jimp");
module.exports = function(context, myBlob) {
const correlation = context.bindings.inputBlob.correlation;
const inputImage = context.bindings.inputBlob.image;
const imageName = context.bindings.inputBlob.imageName;
context.log(
correlation + "Attempting to convert this image to a tiff: " + imageName
);
Jimp.read(Buffer.from(inputImage, "base64"), function(err, image) {
image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
const response = {
myimage: tiff.toString("base64"),
correlation: correlation
};
context.bindings.outputBlob = response
context.log(
correlation + "Successfully converted " + imageName + " to tiff."
);
context.done();
});
});
};
这是 function.json 文件:
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "images-in/{destination}/{name}",
"connection": "AZURE_STORAGE_CONNECTION_STRING"
},
{
"name": "inputBlob",
"type": "blob",
"direction": "in",
"path": "images-in/{destination}/{name}",
"connection": "AZURE_STORAGE_CONNECTION_STRING"
},
{
"type": "blob",
"name": "outputBlob",
"path": "{destination}/{name}.tiff",
"connection": "AZURE_STORAGE_CONNECTION_STRING",
"direction": "out"
}
],
"disabled": false
}
工作方式:
我决定需要对该函数进行http触发,因此进行了以下更改:
const Jimp = require("jimp");
module.exports = function(context, req) {
Jimp.read(Buffer.from(req.body, "base64"), function(err, image) {
image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
const response = {
myimage: tiff.toString("base64"),
correlation: "yeeeeees"
};
});
context.res = {
body: response
};
context.done();
});
};
function.json :
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
但是,我得到 500 :
遥测显示以下内容:
我在做什么错?为什么功能超时?
我对该功能进行了以下更新:
const Jimp = require("jimp");
module.exports = function (context, req) {
const text = Buffer.from(req.body, "base64").toString("utf-8");
Jimp.read(text, function(err, image) {
if (err) {
context.res = {
body: err
};
context.done();
}
image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
if (error) {
context.res = {
body: error
};
context.done();
}
const response = {
myimage: tiff.toString("base64"),
correlation: "yeeeeees"
};
context.res = {
body: response
};
context.done();
});
});
};
这产生了以下荒谬的反应:
{
"errno": -4058,
"code": "ENOENT",
"syscall": "open",
"path": "D:\\home\\site\\wwwroot\\$R\u0005������{\u001av��r��Ū�O�$z�ނ)",
"methodName": "constructor"
}
答案 0 :(得分:2)
如果您检查Azure上的node.js日志,则会看到类似response is not defined
的错误,因为您定义响应的范围不同于您使用的范围。
所以基本上您不能调用context.done();。函数,这就是为什么您的请求抛出超时异常的原因。
使用异步/等待将帮助您避免此类问题。请检查此代码示例以了解可能出问题的地方。
const Jimp = require("jimp");
module.exports = function(context, req) {
Jimp.read(Buffer.from(req.body, "base64"), function(err, image) {
if(err){
context.res = {
body: err
};
context.done();
}
image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
if(error){
context.res = {
body: error
};
context.done();
}
const response = {
myimage: tiff.toString("base64"),
correlation: "yeeeeees"
};
context.res = {
body: response
};
context.done();
});
});
};