英文:
Error: Unexpected end of form > at Multipart._final
问题
这是我首次将NodeJS/ExpressJS API部署到Firebase函数,我遇到了以下错误:
错误:表单意外结束
在Multipart._final(/ ... / functions / node_modules / busboy / lib / types / multipart.js: 588:17)中
在callFinal(node: internal / streams / writable: 696:27)中
在prefinish(node: internal / streams / writable: 725:7)中
在finishMaybe(node: internal / streams / writable: 735:5)中
Multipart.Writable.end(node: internal / streams / writable: 633:5)中
在onend(node: internal / streams / readable: 693:10)中
在processTicksAndRejections(node: internal / process / task_queues: 78:11)中
我试图做的是将图像发送到/upload
端点,然后通过Tensorflow的mobilenet
运行图像,并返回预测数组。我的index.js
代码如下:
const app = express();
const storage = multer.memoryStorage();
app.use(cors());
const upload = multer({ storage });
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Mobile Net Endpoint
let model;
mobilenet.load().then(loadedModel => {
model = loadedModel;
console.info('MobileNet model loaded successfully');
}).catch(error => {
console.error('Error loading MobileNet model:', error);
});
app.post('/upload', upload.single('file'), (req, res) => {
// 处理上传的文件
const file = req.file;
if (!file) {
return res.status(400).json({ message: '未上传文件' });
} else if (file) {
const readImage = () => {
const imageBuffer = file.buffer;
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}
const imageClassification = async () => {
const image = readImage();
const mobilenetModel = await mobilenet.load();
const predictions = await mobilenetModel.classify(image);
return res.json({ predictions });
// console.log('Classification Results:', predictions);
}
imageClassification().then((pred) => {
console.log("pred: ", pred);
}).catch((error) => {
console.log("error: ", error);
return res.status(400).json({ message: error });
})
}
});
我不想将图像存储在Firebase存储或其他地方,因此没有实现这部分。我希望图像在进行移动网络计算时保留在tmp
文件夹中。它在localhost
上运行,其中图像存储在我的硬盘上,所以不确定在Firebase函数中出了什么问题。
英文:
This is the first time I'm deploying a NodeJS/ExpressJS API to Firebase functions and I'm getting this error:
<pre>Error: Unexpected end of form<br> at Multipart._final (/.../functions/node_modules/busboy/lib/types/multipart.js:588:17)<br> at callFinal (node:internal/streams/writable:696:27)<br> at prefinish (node:internal/streams/writable:725:7)<br> at finishMaybe (node:internal/streams/writable:735:5)<br> at Multipart.Writable.end (node:internal/streams/writable:633:5)<br> at onend (node:internal/streams/readable:693:10)<br> at processTicksAndRejections (node:internal/process/task_queues:78:11)</pre>
I'm trying to do is to send an image to the /upload
end-point which takes the photo run it through mobilenet
from Tensorflow and return the predictions array. My Code of index.js
is:
const app = express();
const storage = multer.memoryStorage();
app.use(cors());
const upload = multer({ storage });
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//#region Mobile Net Endpoint
let model;
mobilenet.load().then(loadedModel => {
model = loadedModel;
console.info('MobileNet model loaded successfully');
}).catch(error => {
console.error('Error loading MobileNet model:', error);
});
app.post('/upload', upload.single('file'), (req, res) => {
// Handle the uploaded file here
const file = req.file;
if (!file) {
return res.status(400).json({ message: 'No file uploaded' });
} else if (file) {
const readImage = () => {
const imageBuffer = file.buffer;
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}
const imageClassification = async () => {
const image = readImage();
const mobilenetModel = await mobilenet.load();
const predictions = await mobilenetModel.classify(image);
return res.json({ predictions });
// console.log('Classification Results:', predictions);
}
imageClassification().then((pred) => {
console.log("pred: ", pred);
}).catch((error) => {
console.log("error: ", error);
return res.status(400).json({ message: error });
})
}
});
//#endregion
I don't want to store the image in firebase storage or others that is why there is no implementation of that. I wanted the image to stay in tmp
folder while the predictions are being calculated by mobile net. It works on localhost
where the image is stored in my hard drive so not sure what I'm doing wrong for firebase functions here.
答案1
得分: 3
以下是已翻译的内容:
有几个因素可能导致 Error: Unexpected end of form at Multipart._final...
:
-
存在与
Multer
相关的问题,一些用户建议降级到1.4.2
或1.4.3
,如此GitHub问题链接所述。 -
一些人还提到他们使用了
body-parser
中间件,如Stackoverflow链接所述。 -
另一个Stackoverflow链接指出,将文件定义为
req.body
对他们有用。
这些是一些可行的解决方法。如果上述解决方法未能解决问题,建议通过以下链接之一提交错误报告:
希望这有所帮助。
英文:
There are several factors that may cause Error: Unexpected end of form at Multipart._final...
:
-
There's an ongoing issue with
Multer
and some users suggested downgrading to either1.4.2
or1.4.3
as mentioned in this Github issue link. -
Some also mentioned that they used
body-parser
middleware as mentioned in this Stackoverflow link. -
Another Stackoverflow link stated that defining the file as
req.body
works out for them.
These are some of the workarounds that worked out. In case that the aforementioned workarounds didn't resolve the issue, I would suggest to file a bug through either of the links below:
- Google Cloud issue tracking system and product feature requests
- Firebase bug report
- Multer Github issue link
Hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论