sei-ec-remote / project-2-issues

1 stars 0 forks source link

Adding API data to own mongoDB #125

Closed DRG104 closed 2 years ago

DRG104 commented 2 years ago

What's the problem you're trying to solve?

I am now able to display user queries however I want from the API, onto my liquid files. Now I would like to add the information I'm fetching from the API to my own database. My goal is being able to reference the fetched API data and add, edit or delete them in a user's "list."

Post any code you think might be relevant (one fenced block per file)

Searching through the closed issues, one comment mentioned looking at the comment routes from the Fruits app, but I'm still unsure of what to do

router.post('/:fruitId', (req, res) => {
    // check mongo shell or db.collections to find fruitId or userId
    const fruitId = req.params.fruitId
    req.body.author = req.session.userId

    Fruit.findById(fruitId)
        // after we found a fruit
        // take that fruit and add the comment
        .then(fruit => {
            // single fruit doc there is a field called comments
            fruit.comments.push(req.body)

            // if we change a doc, we HAVE to return and call .save() on the doc
            // mongoose might no longer require 'return', try both
            return fruit.save()
        })
        .then(fruit => {
            res.redirect(`/fruits/${fruit._id}`)
        })
        .catch(err => {
            res.json(err)
        })
})

^^ I believe this is what that comment was referencing. If I'm wrong please disregard.

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

No error message.

What is your best guess as to the source of the problem?

No idea how to add to my own database from API.

What things have you already tried to solve the problem?

I looked up a lot of solutions online, but a lot of them recommend using Atlas. If there is a way to import API data without Atlas, I'd like to know.

Paste a link to your repository here

https://github.com/DRG104/Project-Two

DRG104 commented 2 years ago

This is the comment I was referencing from the closed issue back in March.

"Hey ---, so for those routes you're going to want to interact with your own database -- bc you probably won't be able to update or delete anything from the api. Take a look at how we did comments in the Fruit App and see if you can provide a similar functionality for your app"

kestler01 commented 2 years ago

to save something to your own database you will use the new keyword - checkout how we made fruits in that lesson. Your just going to populate the data for the model with the data you're getting form the api

DRG104 commented 2 years ago

To clarify, do you mean how we created fruits (using the fruits schema from the lesson?) or how we seeded fruits?

If its for seeding, I'm thinking I need to ...

db.on('open', () => {
  // fetch the api here
}

If its like using the fruits schema, I see that my API actually provides schemas with all the keys I could possibly need, I just need to trim out what I don't. I still don't understand how that would save information from my database.

Lastly, is the keyword I should be paying attention to "checkout"? I'm not seeing a reference to checkout IN any js file from the lessons, I just remember it for the mongoshell.

kestler01 commented 2 years ago

I was referring to how we made fruits - but if you wanted to see a whole bunch that's up to you

DRG104 commented 2 years ago

I'm sorry, I think I understand what your saying but I'm not sure how to implement it.

Looking at the lesson we used the Model.create(req.body) inside of a router.post.

I am also using a router.post to fetch my API data. What I want to do is provide some functionality for that API information that is rendered to be added to my database, so that they can be manipulated.