ES6...
ES6 (1) If Else in an Object
오리888
2023. 3. 17. 21:34
Object 안에서 if else를 사용하는 es6 문법에 대해 알아보자
예)
async function handler(req: NextApiRequest, res: NextApiResponse) {
const { email, phone } = req.body; // 유저가 로그인 할시 이메일 또는 폰 번호로 로그인을 시도함
const user = await client.user.upsert({
where: {
...(phone && { phone: +phone }), // if (phone) {phone: +phone} 으로 쓴다
...(email && { email }), // if (email) {email}
},
create: {
name: "Anonymous",
...(phone && { phone: +phone }),
...(email && { email }),
},
update: {},
});
return res.status(200).end();
}
주석을 확인해 보면 &&로 통해 if를 처리할 수 있다