Giter Club home page Giter Club logo

messanger-clone's Introduction

Social Media App

  • MERN stacks

Error..

클라이언트단에서 파일을 전송할때마다 multer를 통해 public에 저장하려는 과정을 구현하다 서버단에서 해당 오류가 나타났다..

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

-api/index.js

const storage = multer.diskStorage({
	destination: (req, file, cb) => {
		cb(null, "public/images");
	},
	filename: (req, file, cb) => {
		cb(null, req.body.name);
	},
});

const upload = multer({ storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
	try {
		return res.status(200).json("File uploded successfully");
	} catch (error) {
		console.error(error);
	}
});

-client/component/share/Share.jsx

const submitHandler = async (e) => {
	e.preventDefault();
	const newPost = {
		userId: user._id,
		desc: desc.current.value,
	};

	if (file) {
		const data = new FormData();
		const filename = Date.now() + file.name;
		/* 정말 어이없게도 기존 폼데이터에 넣는 코드의 순서만 바꿨더니 해결됏다
    data.append("file", file);
		data.append("name", filename);
    */
		data.append("name", filename);
		data.append("file", file);

		newPost.img = filename;
		try {
			await axios.post("/upload", data);
		} catch (error) {
			console.error(error);
		}
	}

	try {
		await axios.post("/posts", newPost);
	} catch (error) {}
};

messanger-clone's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.