错误:在Multipart._final处出现意外的表单结束 >

huangapple go评论77阅读模式
英文:

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> &nbsp; &nbsp;at Multipart._final (/.../functions/node_modules/busboy/lib/types/multipart.js:588:17)<br> &nbsp; &nbsp;at callFinal (node:internal/streams/writable:696:27)<br> &nbsp; &nbsp;at prefinish (node:internal/streams/writable:725:7)<br> &nbsp; &nbsp;at finishMaybe (node:internal/streams/writable:735:5)<br> &nbsp; &nbsp;at Multipart.Writable.end (node:internal/streams/writable:633:5)<br> &nbsp; &nbsp;at onend (node:internal/streams/readable:693:10)<br> &nbsp; &nbsp;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 =&gt; {
model = loadedModel;
console.info(&#39;MobileNet model loaded successfully&#39;);
}).catch(error =&gt; {
console.error(&#39;Error loading MobileNet model:&#39;, error);
});
app.post(&#39;/upload&#39;, upload.single(&#39;file&#39;), (req, res) =&gt; {
// Handle the uploaded file here
const file = req.file;
if (!file) {
return res.status(400).json({ message: &#39;No file uploaded&#39; });
} else if (file) {
const readImage = () =&gt; {
const imageBuffer = file.buffer;
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}
const imageClassification = async () =&gt; {
const image = readImage();
const mobilenetModel = await mobilenet.load();
const predictions = await mobilenetModel.classify(image);
return res.json({ predictions });
// console.log(&#39;Classification Results:&#39;, predictions);
}
imageClassification().then((pred) =&gt; {
console.log(&quot;pred: &quot;, pred);
}).catch((error) =&gt; {
console.log(&quot;error: &quot;, 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.21.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 either 1.4.2 or 1.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:

Hope this helps.

huangapple
  • 本文由 发表于 2023年6月26日 22:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定