-1

I want my data object to be like:

{ email: "x", password: "x" }

Logging data brings me: [ Promise { undefined }, Promise { <pending> } ]

What do I have to change to get the desired result?

const controller = (modelName: string, fields: string[]) => {
    const createOne = (req: Request, res: Response) => {
        wrapper(req, res, async (prisma: PrismaClient) => {
            const data = fields.map(async (field) => {
                if (field in req.body) {
                    if (field === 'password') {
                        return { [field]: await hash(req.body[field], await genSalt(10)) }
                    }
                } else {
                    return { [field]: req.body[field] }
                }
            })
            console.log(data)
            // @ts-ignore
            return await prisma[modelName].create({ data })
        })
    }
    ...
}

1 Answers1

-1

fields.map(async (field) => {/* ... */}) will return an array of promises. You can use Promise.all to await all of the values in one promise:

const mappedValues = await Promise.all(fields.map(async (field) => {/* ... */})
jsejcksn
  • 27,667
  • 4
  • 38
  • 62