Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

typescript - createreadstream is not a function + graphql-upload

I'm trying to upload a file from Angular using nodejs, express and graphql-upload. I coded and when I try to upload, my server says that createreadstream is not a function and I don't know where is the problem. I watched the same questions but older. Please somebody can help me? If you need more information, please, let me know. I'm' junior asking. Thank you in advance

Nodejs version 14.13.1

Grapql-upload latest

typescript

My code is the next one:

Mutations.ts

    import { IResolvers } from 'graphql-tools';
    import fs from 'fs'
    import { processUpload } from '../../lib/upload';
    
    const resolversColorMutation: IResolvers = {
    
      Mutation: {
        
       
        singleUpload: (_, { file }, { cloudinary }) => processUpload(file, cloudinary)
    
        }
      }
    
export default resolversColorMutation

upload.ts

const storeFyleSystem = ({ stream, filename }: any, userId: string) => {
    const path = `${UPLOAD_DIR}/${filename}`;
    const id = userId;
    return new Promise((resolve, reject) =>
        stream
            .on('error', (error: any) => {
                if (stream.truncated) {
                    // Delete the truncated file.
                    // Borar el fichero local
                    fs.unlinkSync(path);
                }
                reject(error);
            })
            .pipe(fs.createWriteStream(path))
            .on('error', (error: any) => reject(error))
            .on('finish', () => resolve({ id, path }))
    );
};


const UPLOAD_DIR = './uploads';


export const processUpload = async (upload: any, cloudinary: any) => {
    mkdirp.sync(UPLOAD_DIR);
    const { createReadStream, filename, mimetype } = await upload;
    **const stream = createReadStream()**;
    const { id, path }: any = await storeFyleSystem({ stream, filename }, 'theName');

    fs.unlinkSync(path);

    return { id, path, filename, mimetype};
};
question from:https://stackoverflow.com/questions/65873980/createreadstream-is-not-a-function-graphql-upload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...