Coder Social home page Coder Social logo

petshop_node_sql_express_alura's Introduction

Hi! Welcome to my Github.

I'm a lawyer who decided to change career, and have been studying tirelessly to become a back-end web developer. I also like to read about computer stuff in my freetime, like OS and computer networks, it really fascinates me.

I'm currently learning to code in Node.JS, and have been building a few apps and services to train back-end development core concepts.

I also know a bit about Python and Django, and I can do some things with Java as well.

Here you can find some things I've already made, I'll make those repositories grow (almost) every single day.

petshop_node_sql_express_alura's People

Contributors

edemuner avatar

Watchers

 avatar

petshop_node_sql_express_alura's Issues

TypeError: result.setPet is not a function

So, I'm new to Node.JS, and this project is a simple petshop, with pets table, services offered, and appointments (when a certain service is applied to a certain pet in a certain date in the future).

Appointments have a petId and serviceId, each appointment must have only one of those, but pets and services can be referenced in multiple appointments.

This project was originally made with pure MySql, with separate repositories to handle queries, later I changed it to use sequelize ORM.

I've made the basics crud operations using the rest architecture, everything ok with the services and pets methods (post, get, patch, delete), but the problem began when trying to make post method with appointments, since appointments have serviceId and petId.

index.js

const customExpress = require('./config/customExpress');
const connection = require('./infrastructure/database/connection');


connection.sync()
.then(() => {`
  console.log('Successfully connected to database')
  const app = customExpress()
  app.listen(3000, () => console.log('Server running at port 3000'))
})
.catch(erro => console.log('Something went wrong while trying to synchronize to database'))

connection.js
here is where the associations between tables are made, I think the error might be here, but can't figure out where

const Sequelize = require('sequelize')
const config = require('config')
const { applyExtraSetup } = require('../../models/extraSetup')


const sequelize = new Sequelize(
    config.get('mysql.database'),
    config.get('mysql.user'),
    config.get('mysql.password'),
    {
    host:config.get('mysql.host'),
    dialect:config.get('mysql.dialect')
    }
)

const modelDefiners = [
    require('../../models/appointments'),
    require('../../models/pets'),
    require('../../models/services'),
]
// it gets the models definitions and pass the above instance of sequelize to them
for (const modelDefiner of modelDefiners){
    modelDefiner(sequelize)
}
// and then make their relations with extra setup
applyExtraSetup(sequelize)
.then(module.exports = sequelize)

and this is appointment.js, a controller that handles the /appointments route and call the sequelize methods

app.post('/appointment', async (req, res) => {

        const data = req.body
        const pet = await Pets.findAll({ where : { name : req.body.petId }})
        const service = await Service.findAll({ where : { name : req.body.serviceId }})
        const appointment = Appointment.create(data)
        .then(result => {
                result.setPet(pet)
                res.end()

            })

reading sequelize documentation, I saw that with this type of association, you get magic methods to handle data from the related tables, which you call from an instance of a model (setPet, getPet, and so on).

Can anyone help me?

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.