1

I would like to connect to the postgreSQL database.

I use the tutorial: http://docs.sequelizejs.com/manual/installation/getting-started

I do not know where to put the code to connect to the database?

import {Sequelize} from '@angular/sequelize';
const Sequelize = require('sequelize'); 
const sequelize = new Sequelize('postgres://localhost:5432/neme_database');

sequelize.authenticate().then(() => {
 console.log('Connection has been established successfully.');
})
.catch(err => {
  console.error('Unable to connect to the database:', err);
});


const UserLogin = sequelize.define('user', {
  userName: {
    type: Sequelize.TEXT
  },
  password: {
    type: Sequelize.TEXT
  },
});


UserLogin.findAll().then(users => {
   console.log(users)
});
1
  • I would like to use the UserLogin.findAll() method in login/login.component.ts . I installed only: npm install sequelize Commented Jan 20, 2018 at 2:37

2 Answers 2

1

I think you need to split your code into at least three files:

config.js

import {Sequelize} from '@angular/sequelize';
const Sequelize = require('sequelize'); 
const sequelize = new Sequelize('postgres://localhost:5432/neme_database');

export sequlize

in you index.js you import your config and connect to database

import { sequlize } from 'path-to-config/config.js`

sequelize.authenticate().then(() => {
 console.log('Connection has been established successfully.');
})
.catch(err => {
  console.error('Unable to connect to the database:', err);
});

user models models/user.js

export const UserLogin = sequelize.define('user', {
  userName: {
    type: Sequelize.TEXT
  },
  password: {
    type: Sequelize.TEXT
  },
});

Then in your controller inside function such as

getAllUser() {
  return UserLogin.findAll()
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry I added the question wrong. Is above Jerry Chen's response
0

Code now has the following structure:

- app
 -- auth
  --- auth.quard.ts
  --- auth.service.ts
  --- user.ts
 - home
  --- home.component.html
  --- home.component.ts
 - login  
  --- login.component.html
  --- login.component.ts
 - models
  --- UserLogin.js
 - app.routing.models.ts
 - app.components.ts
 - app.components.html
 - app.models.ts
 - config.js
 - index.js

Should I install to connect to the database?

        npm install express --save       
        npm install passport --save          
        npm install passport-local --save            
        npm install body-parser --save           
        npm install express-session --save           
        npm install bcrypt-nodejs --save             
        npm install express-handlebars --save

I put the getAllUser method in auth / auth.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { User } from './user';
import {UserLogin}  from './models/userLogin.js';


@Injectable()

export class AuthService {

  private loggedIn = new BehaviorSubject<boolean>(false);   

  get isLoggedIn() {
    return this.loggedIn.asObservable();
  }

  constructor(
    private router: Router
  ) {}

  login(user: User) {
    if (user.userName !== '' && user.password !== '' )  {
      this.loggedIn.next(true);       
      this.router.navigate(['/']);
    }
  }

  getAllUser() {
   return UserLogin.findAll();
  }
}

but it returns an error message:

 Failed to compile.

./src/app/auth/auth.service.ts
Module not found: Error: Can't resolve './models/userLogin.js' in 
'C:\myPath\name_project\src\app\auth'
@ ./src/app/auth/auth.service.ts 15:21-53
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts

app/models/userLogin.js:

    const UserLogin = sequelize.define('user', {
        userName: {
            type: Sequelize.STRING
          },
        password: {
            type: Sequelize.STRING
          },
    });


    UserLogin.findAll().then(users => {
          console.log(users)
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.