Express Graphql如何上载多个文件而不是一个文件
大家好,我有一个用于上传图像的graphql解析器。问题是,我想上传多个图像到我不断得到一个错误。这是我的密码。在graphql服务器上上载多个图像的正确方法是什么 分解器Express Graphql如何上载多个文件而不是一个文件,express,upload,graphql,Express,Upload,Graphql,大家好,我有一个用于上传图像的graphql解析器。问题是,我想上传多个图像到我不断得到一个错误。这是我的密码。在graphql服务器上上载多个图像的正确方法是什么 分解器 export const uploadImage = async (_, { files }, context) => { await jwtAuthentication.verifyTokenMiddleware(context); console.log(file); try { let fi
export const uploadImage = async (_, { files }, context) => {
await jwtAuthentication.verifyTokenMiddleware(context);
console.log(file);
try {
let fileArr = [];
for (let i = 0; i < files.length; i++) {
const stream = createReadStream();
const pathname = path.join(__dirname, `/public/images/${files[i].filename}`);
await stream.pipe(fs.createWriteStream(files[i].pathname));
fileArr.push({ path: `${apiUrl}/images/${files[i].filename}` });
}
//save image to document
return fileArr;
} catch (error) {
console.log(error);
// graphqlError(error);
}
};
前端查询
export const UPLOAD_IMAGE = gql`
mutation($files: [Upload]) {
uploadImage(files: $files)
}
`;
前端上传功能
const uploadFiles = async (): Promise<void> => {
try {
const response = await GraphqlRequest(auth.token).request(UPLOAD_IMAGE, { files });
console.log(response);
} catch (err) {
console.log(err);
}
};
constuploadfiles=async():Promise=>{
试一试{
const response=wait GraphqlRequest(auth.token).request(上传图片,{files});
控制台日志(响应);
}捕捉(错误){
控制台日志(err);
}
};
什么错误/哪里?什么错误/哪里?
const uploadFiles = async (): Promise<void> => {
try {
const response = await GraphqlRequest(auth.token).request(UPLOAD_IMAGE, { files });
console.log(response);
} catch (err) {
console.log(err);
}
};