此问题已在此处有答案:
API resolved without sending a response for /api/repositories, this may result in stalled requests(1个答案)
3天前关闭。
我实现了一个简单的页面和API来上传一个csv文件。文件成功加载并使用API存储在服务器上,但我得到此错误:
API resolved without sending a response for /api/uploadv2, this may result in stalled requests.
我已经检查了我的代码,我正在返回所有内容的响应。
虽然我没有实现详细的错误处理条件,因为这只是一个实践实现。我无法确定此警告/错误是从何处触发的。
我尝试的是下面的API代码:
// pages/api/upload.js
import multer from 'multer';
import csv from 'csv-parser';
import { createReadStream, mkdirSync, existsSync } from 'fs';
import path from 'path';
import moment from 'moment';
import { promisify } from 'util';
import { pipeline } from 'stream';
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const dateFolder = moment().format('YYYY-MM-DD_HH-mm-ss');
const dir = path.join('uploads', dateFolder);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
cb(null, dir);
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
export const config = {
api: {
bodyParser: false,
},
};
async function parseCsv(file) {
const results = [];
const stream = createReadStream(file.path);
const parser = csv();
const asyncPipeline = promisify(pipeline);
await asyncPipeline(stream, parser);
parser.on('data', (data) => results.push(data));
return results;
}
export default async function uploadCsv(req, res) {
upload.single('file')(req, {}, async function (err) {
if (err) {
return res.status(400).json({ error: err.message });
}
const { file } = req;
if (!file) {
return res.status(400).json({ error: 'Please upload a file' });
}
try {
const data = await parseCsv(file);
return res.status(200).json({ data });
} catch (err) {
return res.status(500).json({ error: 'Error parsing CSV file' });
}
});
}
我期望数据将在数据对象中返回,但也是空白的
browser screenshot
1条答案
按热度按时间yfjy0ee71#
我可以简单地通过添加到API配置externalResolver来修复它:代码如下: