D
D
ddgryaz2021-05-01 19:39:01
Upwork
ddgryaz, 2021-05-01 19:39:01

How to get value from an object?

Good day!
There is the following piece of code:

async create(req, res, next) {
        try {
            let {name, info} = req.body
            const {img} = req.files
            let fileName = uuid.v4() + ".jpg"
            img.mv(path.resolve(__dirname, '..', 'static', fileName))
            const brain = await Brain.create({name, img: fileName})

            if (info) {
                info = JSON.parse(info)
                Object.keys(info).forEach(i => console.log(i))
                // Object.keys(info).forEach(i =>
                //     Brain_info.create({
                //         title: i.title,
                //         description: i.description,
                //         brainId: brain.id
                //     })
                // )
            }

            return res.json(brain)
        } catch (e) {
            next(ApiError.badRequest(e.message))
        }

    }


In general, what I tried to do is commented out.
When passing the info object, create a record in the database with the passed fields.
in the request under info - I pass JSON
{
"title": "spad",
"description": "dsadsadsadsad"
}


If I do console.log(i.title)

Then I get undefined.
How to do it right? I need to pass the value of the title and description passed in JSON to the commented out part of the code.

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Puma Thailand, 2018-09-24
@opium

Identity verification does not depend on the design of the profile.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question