title: Argument of type 'users | undefined' is not assignable to parameter of type 'users'. Type 'undefined' is not assignable to type 'users'
body: <p>Im trying to create a put method with TYPEORM and don't understand how can I fix this error and why. This request a typeof? but my users has a value. (im newbie, sure).</p>
  <p>Anyone can help me? how can I fix it?</p>
  <p>When  :</p>
  <pre class="lang-ts prettyprint-override"><code> const user: any = await usersRepository.findOne(req.params.id)
  </code></pre>
  <p>so:
  no problem
  (why)</p>
  <p><strong>ERROR:</strong></p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;resource&quot;: &quot;/Users/camargo/Documents/Estudo/nodejs-1/source/server.ts&quot;,
      &quot;owner&quot;: &quot;typescript&quot;,
      &quot;code&quot;: &quot;2345&quot;,
      &quot;severity&quot;: 8,
      &quot;message&quot;: &quot;Argument of type 'users | undefined' is not assignable to parameter of type 'users'.\n  Type 'undefined' is not assignable to type 'users'.&quot;,
      &quot;source&quot;: &quot;ts&quot;,
      &quot;startLineNumber&quot;: 71,
      &quot;startColumn&quot;: 31,
      &quot;endLineNumber&quot;: 71,
      &quot;endColumn&quot;: 35
  }
  </code></pre>
  <p>CODE</p>
  <pre class="lang-ts prettyprint-override"><code>    ** source/server.ts */
      import http from 'http';
      import { connection } from &quot;./connection/connection&quot;
      import express, { Express } from 'express';
      import morgan from 'morgan';
      import routes from './routes/posts';
      
      
      import cors from &quot;cors&quot;;
      import { users } from './entities/users';
      
      // Setando rotas express
      
      const router: Express = express();
      const app=express()
      app.use(cors())
      app.use(express.json())
      const server=app.listen(3000,()=&gt;{
          console.log(&quot;Server rodando... 3000....&quot;)
      })
      app.get(&quot;/api&quot;,(req,res)=&gt;{
          res.send(&quot;API - Bem Vindo&quot;)
      })
      connection.then(
          async connection=&gt;{
              console.log(&quot;Conectado&quot;)
            
        const usersRepository = connection.getRepository(users);
      
        app.get(&quot;/api/users/:id&quot;,async(req,res)=&gt;{
      
          /* Metodos */
      
          //METODO GET 
      
          const user = await usersRepository.findOne({where: { id: req.params.id }})
          res.json({
              message:&quot;success&quot;,
              payload: user
          })
      })
      
          //  METODO POST
      
      app.post(&quot;/api/users&quot;,async (req,res)=&gt;{
                  
          console.log(&quot;body&quot;,req.body)
          const user = await usersRepository.create(req.body)
          const results = await usersRepository.save(user);
          
      res.json({
      message: &quot;success&quot;,
      payload: results
      });
      })
      
          // METODO DELETE
      
          app.delete(&quot;/api/users/:id&quot;,async(req,res)=&gt;{
              const user = await usersRepository.delete(req.params.id)
              res.json({
                  message:&quot;success&quot;,
              })
          })
      
             
              //METODO PUT
      
          app.put(&quot;/api/users/:id&quot;,async(req,res)=&gt;{
              const user = await usersRepository.findOne(req.params.id)
              usersRepository.merge(user, req.body);
              const result = await usersRepository.save(user);
              res.json({
                  message:&quot;success&quot;,
                  payload:result
              })
        
          })
              
          
      }
      ).catch(error=&gt;{
          console.log(error)
          
      })
      
      
      /** Conexao teste morgan */
      router.use(morgan('dev'));
      /** Parse the req */
      router.use(express.urlencoded({ extended: false }));
      /** Takes care of JSON data */
      router.use(express.json());
      
      /** Regras da API*/
      router.use((req, res, next) =&gt; {
          // CORS 
          res.header('Access-Control-Allow-Origin', '*');
          // CORS headers
          res.header('Access-Control-Allow-Headers', 'origin, X-Requested-With,Content-Type,Accept, Authorization');
          // CORS metodos
          if (req.method === 'OPTIONS') {
              res.header('Access-Control-Allow-Methods', 'GET PATCH DELETE POST');
              return res.status(200).json({});
          }
          next();
      });
      
      /** Routes */
      router.use('/', routes);
      
      /** Error handling */
      router.use((req, res, next) =&gt; {
          const error = new Error('not found');
          return res.status(404).json({
              message: error.message
          });
      });
      
      /** Server */
      
      const httpServer = http.createServer(router);
      const PORT: any = process.env.PORT ?? 6060;
      httpServer.listen(PORT, () =&gt; console.log(`Server rodando em ${PORT}`));
  </code></pre>