How to use sequelize to fetch object data?How can I prevent SQL injection in PHP?Should I use the datetime or timestamp data type in MySQL?Convert JS object to JSON stringHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsHow do I update each dependency in package.json to the latest version?sequelize - Cannot add foreign key constraintComposite primary key in junction table - SequelizeSequelize/MySQL setting up multiple tables

In the Marvel universe, can a human have a baby with any non-human?

Intuitively, why does putting capacitors in series decrease the equivalent capacitance?

How to reply to small talk/random facts in a non-offensive way?

What is the legal status of travelling with (unprescribed) methadone in your carry-on?

MH370 blackbox - is it still possible to retrieve data from it?

Why is Madam Hooch not a professor?

Peace Arch without exiting USA

Does ultrasonic bath cleaning damage laboratory volumetric glassware calibration?

What happens when I sacrifice a creature when my Teysa Karlov is on the battlefield?

What sort of mathematical problems are there in AI that people are working on?

What are the benefits of using the X Card safety tool in comparison to plain communication?

How to get cool night-vision without lame drawbacks?

Why do some games show lights shine through walls?

How can I repair scratches on a painted French door?

Impossible darts scores

How come I was asked by a CBP officer why I was in the US?

Why is there no havdallah when going from Yom Tov into Shabbat?

Why would people reject a god's purely beneficial blessing?

Why is the G major to Bb major resolution so strong?

Why doesn't a marching band have strings?

Using “sparkling” as a diminutive of “spark” in a poem

Is adding a new player (or players) a DM decision, or a group decision?

Why do textbooks often include the solutions to odd or even numbered problems but not both?

Low-gravity Bronze Age fortifications



How to use sequelize to fetch object data?


How can I prevent SQL injection in PHP?Should I use the datetime or timestamp data type in MySQL?Convert JS object to JSON stringHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsHow do I update each dependency in package.json to the latest version?sequelize - Cannot add foreign key constraintComposite primary key in junction table - SequelizeSequelize/MySQL setting up multiple tables






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to implement Sequelize as an ORM in NodeJs and I am using it for Mysql,
I have 3 tables in the sample -


1. Role

2. User (Has a role)

3. Code (IsCreated by a user)



I'm unable to query the tables/models properly,

As I should be receiving an model representation of a table, which is referred as a foreign key.



Following is my DB structure -
1. Role table -
enter image description here



2. User table -
enter image description here



3. Code table -
enter image description here



Following are the table creation queries -
1. Role -
CREATE TABLE role (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
PRIMARY KEY (id)
);



2. User -



CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`role` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `role` (`role`),
CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`id`)
);


3. Code -



CREATE TABLE `code` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`createdBy` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `createdBy` (`createdBy`),
CONSTRAINT `code_ibfk_1` FOREIGN KEY (`createdBy`) REFERENCES `user` (`id`)
);


Following is my app.js file -



 const db = require('./db');
const Code = require('./code');
const User = require('./user');
const Role = require('./role');
const async = require('async');


async.waterfall([
function(callback)
db
.authenticate()
.then(() =>
console.log('Connection has been established successfully.');
callback(null,"ok");
)
.catch(err =>
return callback(err,null);
);
,
function(resp,callback)
Code.findAll(include: [ model: User])
.then(code =>
console.log("All users:", JSON.stringify(code, null, 4));
callback(null,"ok");
)
.catch(err =>
callback(err,null);
);

// Code.findOne(
// where:
// id: 1
//
// )
// .then(code =>
// console.log("All users:", JSON.stringify(code, null, 4));
// )
// .catch(err => console.log("Error => n",err));

,
],
function(err, resp)
if (err)
console.log(err);
else
console.log(resp);

);


Following is my db.js file -



const Sequelize = require('sequelize');

module.exports = new Sequelize('junit', 'root', 'root', 'postgres' );


Following is my role.js file -



const Sequelize = require('sequelize');
const db = require('./db');
const User = require('./user');

const Role = db.define('role',
id:
type: Sequelize.INTEGER,
primaryKey: true,
allowNull:false,
autoIncrement: true
,
name:
type: Sequelize.STRING

,
tableName: 'role',
freezeTableName: true,
timestamps: false
);

associate : (models) =>
Role.hasMany(models.User,
foreignKey: 'role'
);
;

module.exports = Role;


Following is my user.js file -



const Sequelize = require('sequelize');
const db = require('./db');
const Code = require('./code');
const Role = require('./role');

const User = db.define('user',
id:
type: Sequelize.INTEGER,
primaryKey: true,
allowNull:false,
autoIncrement: true
,
name:
type: Sequelize.STRING
,
email:
type: Sequelize.STRING
,
role:
type: Sequelize.INTEGER,
references:
// This is a reference to another model
model: Role,

// This is the column name of the referenced model
key: 'id'


,
tableName: 'user',
freezeTableName: true,
timestamps: false
);

associate : (models) =>
User.hasMany(models.Code,
foreignKey: 'createdBy'
);

User.belongsTo(models.Role,
foreignKey: 'role'
);
;

module.exports = User;


Following is my code.js file -



const Sequelize = require('sequelize');
const db = require('./db');
const User = require('./user');
//one-to-many
const Code = db.define('code',
id:
type: Sequelize.INTEGER,
primaryKey: true,
allowNull:false,
autoIncrement: true
,
name:
type: Sequelize.STRING
,
// createdBy:
// type: Sequelize.INTEGER
// ,
createdBy:
type: Sequelize.INTEGER,
references:
// This is a reference to another model
model: User,

// This is the column name of the referenced model
key: 'id'


,
tableName: 'code',
freezeTableName: true,
timestamps: false
);

associate : (models) =>
Code.belongsTo(models.User,
foreignKey: 'createdBy'
);
;

module.exports = Code;


When I run the app.js file I can't see the model reference of User,

But I get the usual Integer value, Can someone please help on how to properly use the Model here?



Error trace -
enter image description here










share|improve this question






























    0















    I am trying to implement Sequelize as an ORM in NodeJs and I am using it for Mysql,
    I have 3 tables in the sample -


    1. Role

    2. User (Has a role)

    3. Code (IsCreated by a user)



    I'm unable to query the tables/models properly,

    As I should be receiving an model representation of a table, which is referred as a foreign key.



    Following is my DB structure -
    1. Role table -
    enter image description here



    2. User table -
    enter image description here



    3. Code table -
    enter image description here



    Following are the table creation queries -
    1. Role -
    CREATE TABLE role (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    PRIMARY KEY (id)
    );



    2. User -



    CREATE TABLE `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `email` varchar(100) NOT NULL,
    `role` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `role` (`role`),
    CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`id`)
    );


    3. Code -



    CREATE TABLE `code` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `createdBy` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `createdBy` (`createdBy`),
    CONSTRAINT `code_ibfk_1` FOREIGN KEY (`createdBy`) REFERENCES `user` (`id`)
    );


    Following is my app.js file -



     const db = require('./db');
    const Code = require('./code');
    const User = require('./user');
    const Role = require('./role');
    const async = require('async');


    async.waterfall([
    function(callback)
    db
    .authenticate()
    .then(() =>
    console.log('Connection has been established successfully.');
    callback(null,"ok");
    )
    .catch(err =>
    return callback(err,null);
    );
    ,
    function(resp,callback)
    Code.findAll(include: [ model: User])
    .then(code =>
    console.log("All users:", JSON.stringify(code, null, 4));
    callback(null,"ok");
    )
    .catch(err =>
    callback(err,null);
    );

    // Code.findOne(
    // where:
    // id: 1
    //
    // )
    // .then(code =>
    // console.log("All users:", JSON.stringify(code, null, 4));
    // )
    // .catch(err => console.log("Error => n",err));

    ,
    ],
    function(err, resp)
    if (err)
    console.log(err);
    else
    console.log(resp);

    );


    Following is my db.js file -



    const Sequelize = require('sequelize');

    module.exports = new Sequelize('junit', 'root', 'root', 'postgres' );


    Following is my role.js file -



    const Sequelize = require('sequelize');
    const db = require('./db');
    const User = require('./user');

    const Role = db.define('role',
    id:
    type: Sequelize.INTEGER,
    primaryKey: true,
    allowNull:false,
    autoIncrement: true
    ,
    name:
    type: Sequelize.STRING

    ,
    tableName: 'role',
    freezeTableName: true,
    timestamps: false
    );

    associate : (models) =>
    Role.hasMany(models.User,
    foreignKey: 'role'
    );
    ;

    module.exports = Role;


    Following is my user.js file -



    const Sequelize = require('sequelize');
    const db = require('./db');
    const Code = require('./code');
    const Role = require('./role');

    const User = db.define('user',
    id:
    type: Sequelize.INTEGER,
    primaryKey: true,
    allowNull:false,
    autoIncrement: true
    ,
    name:
    type: Sequelize.STRING
    ,
    email:
    type: Sequelize.STRING
    ,
    role:
    type: Sequelize.INTEGER,
    references:
    // This is a reference to another model
    model: Role,

    // This is the column name of the referenced model
    key: 'id'


    ,
    tableName: 'user',
    freezeTableName: true,
    timestamps: false
    );

    associate : (models) =>
    User.hasMany(models.Code,
    foreignKey: 'createdBy'
    );

    User.belongsTo(models.Role,
    foreignKey: 'role'
    );
    ;

    module.exports = User;


    Following is my code.js file -



    const Sequelize = require('sequelize');
    const db = require('./db');
    const User = require('./user');
    //one-to-many
    const Code = db.define('code',
    id:
    type: Sequelize.INTEGER,
    primaryKey: true,
    allowNull:false,
    autoIncrement: true
    ,
    name:
    type: Sequelize.STRING
    ,
    // createdBy:
    // type: Sequelize.INTEGER
    // ,
    createdBy:
    type: Sequelize.INTEGER,
    references:
    // This is a reference to another model
    model: User,

    // This is the column name of the referenced model
    key: 'id'


    ,
    tableName: 'code',
    freezeTableName: true,
    timestamps: false
    );

    associate : (models) =>
    Code.belongsTo(models.User,
    foreignKey: 'createdBy'
    );
    ;

    module.exports = Code;


    When I run the app.js file I can't see the model reference of User,

    But I get the usual Integer value, Can someone please help on how to properly use the Model here?



    Error trace -
    enter image description here










    share|improve this question


























      0












      0








      0








      I am trying to implement Sequelize as an ORM in NodeJs and I am using it for Mysql,
      I have 3 tables in the sample -


      1. Role

      2. User (Has a role)

      3. Code (IsCreated by a user)



      I'm unable to query the tables/models properly,

      As I should be receiving an model representation of a table, which is referred as a foreign key.



      Following is my DB structure -
      1. Role table -
      enter image description here



      2. User table -
      enter image description here



      3. Code table -
      enter image description here



      Following are the table creation queries -
      1. Role -
      CREATE TABLE role (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(100) NOT NULL,
      PRIMARY KEY (id)
      );



      2. User -



      CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `email` varchar(100) NOT NULL,
      `role` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `role` (`role`),
      CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`id`)
      );


      3. Code -



      CREATE TABLE `code` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `createdBy` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `createdBy` (`createdBy`),
      CONSTRAINT `code_ibfk_1` FOREIGN KEY (`createdBy`) REFERENCES `user` (`id`)
      );


      Following is my app.js file -



       const db = require('./db');
      const Code = require('./code');
      const User = require('./user');
      const Role = require('./role');
      const async = require('async');


      async.waterfall([
      function(callback)
      db
      .authenticate()
      .then(() =>
      console.log('Connection has been established successfully.');
      callback(null,"ok");
      )
      .catch(err =>
      return callback(err,null);
      );
      ,
      function(resp,callback)
      Code.findAll(include: [ model: User])
      .then(code =>
      console.log("All users:", JSON.stringify(code, null, 4));
      callback(null,"ok");
      )
      .catch(err =>
      callback(err,null);
      );

      // Code.findOne(
      // where:
      // id: 1
      //
      // )
      // .then(code =>
      // console.log("All users:", JSON.stringify(code, null, 4));
      // )
      // .catch(err => console.log("Error => n",err));

      ,
      ],
      function(err, resp)
      if (err)
      console.log(err);
      else
      console.log(resp);

      );


      Following is my db.js file -



      const Sequelize = require('sequelize');

      module.exports = new Sequelize('junit', 'root', 'root', 'postgres' );


      Following is my role.js file -



      const Sequelize = require('sequelize');
      const db = require('./db');
      const User = require('./user');

      const Role = db.define('role',
      id:
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
      ,
      name:
      type: Sequelize.STRING

      ,
      tableName: 'role',
      freezeTableName: true,
      timestamps: false
      );

      associate : (models) =>
      Role.hasMany(models.User,
      foreignKey: 'role'
      );
      ;

      module.exports = Role;


      Following is my user.js file -



      const Sequelize = require('sequelize');
      const db = require('./db');
      const Code = require('./code');
      const Role = require('./role');

      const User = db.define('user',
      id:
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
      ,
      name:
      type: Sequelize.STRING
      ,
      email:
      type: Sequelize.STRING
      ,
      role:
      type: Sequelize.INTEGER,
      references:
      // This is a reference to another model
      model: Role,

      // This is the column name of the referenced model
      key: 'id'


      ,
      tableName: 'user',
      freezeTableName: true,
      timestamps: false
      );

      associate : (models) =>
      User.hasMany(models.Code,
      foreignKey: 'createdBy'
      );

      User.belongsTo(models.Role,
      foreignKey: 'role'
      );
      ;

      module.exports = User;


      Following is my code.js file -



      const Sequelize = require('sequelize');
      const db = require('./db');
      const User = require('./user');
      //one-to-many
      const Code = db.define('code',
      id:
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
      ,
      name:
      type: Sequelize.STRING
      ,
      // createdBy:
      // type: Sequelize.INTEGER
      // ,
      createdBy:
      type: Sequelize.INTEGER,
      references:
      // This is a reference to another model
      model: User,

      // This is the column name of the referenced model
      key: 'id'


      ,
      tableName: 'code',
      freezeTableName: true,
      timestamps: false
      );

      associate : (models) =>
      Code.belongsTo(models.User,
      foreignKey: 'createdBy'
      );
      ;

      module.exports = Code;


      When I run the app.js file I can't see the model reference of User,

      But I get the usual Integer value, Can someone please help on how to properly use the Model here?



      Error trace -
      enter image description here










      share|improve this question
















      I am trying to implement Sequelize as an ORM in NodeJs and I am using it for Mysql,
      I have 3 tables in the sample -


      1. Role

      2. User (Has a role)

      3. Code (IsCreated by a user)



      I'm unable to query the tables/models properly,

      As I should be receiving an model representation of a table, which is referred as a foreign key.



      Following is my DB structure -
      1. Role table -
      enter image description here



      2. User table -
      enter image description here



      3. Code table -
      enter image description here



      Following are the table creation queries -
      1. Role -
      CREATE TABLE role (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(100) NOT NULL,
      PRIMARY KEY (id)
      );



      2. User -



      CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `email` varchar(100) NOT NULL,
      `role` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `role` (`role`),
      CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`id`)
      );


      3. Code -



      CREATE TABLE `code` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `createdBy` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `createdBy` (`createdBy`),
      CONSTRAINT `code_ibfk_1` FOREIGN KEY (`createdBy`) REFERENCES `user` (`id`)
      );


      Following is my app.js file -



       const db = require('./db');
      const Code = require('./code');
      const User = require('./user');
      const Role = require('./role');
      const async = require('async');


      async.waterfall([
      function(callback)
      db
      .authenticate()
      .then(() =>
      console.log('Connection has been established successfully.');
      callback(null,"ok");
      )
      .catch(err =>
      return callback(err,null);
      );
      ,
      function(resp,callback)
      Code.findAll(include: [ model: User])
      .then(code =>
      console.log("All users:", JSON.stringify(code, null, 4));
      callback(null,"ok");
      )
      .catch(err =>
      callback(err,null);
      );

      // Code.findOne(
      // where:
      // id: 1
      //
      // )
      // .then(code =>
      // console.log("All users:", JSON.stringify(code, null, 4));
      // )
      // .catch(err => console.log("Error => n",err));

      ,
      ],
      function(err, resp)
      if (err)
      console.log(err);
      else
      console.log(resp);

      );


      Following is my db.js file -



      const Sequelize = require('sequelize');

      module.exports = new Sequelize('junit', 'root', 'root', 'postgres' );


      Following is my role.js file -



      const Sequelize = require('sequelize');
      const db = require('./db');
      const User = require('./user');

      const Role = db.define('role',
      id:
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
      ,
      name:
      type: Sequelize.STRING

      ,
      tableName: 'role',
      freezeTableName: true,
      timestamps: false
      );

      associate : (models) =>
      Role.hasMany(models.User,
      foreignKey: 'role'
      );
      ;

      module.exports = Role;


      Following is my user.js file -



      const Sequelize = require('sequelize');
      const db = require('./db');
      const Code = require('./code');
      const Role = require('./role');

      const User = db.define('user',
      id:
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
      ,
      name:
      type: Sequelize.STRING
      ,
      email:
      type: Sequelize.STRING
      ,
      role:
      type: Sequelize.INTEGER,
      references:
      // This is a reference to another model
      model: Role,

      // This is the column name of the referenced model
      key: 'id'


      ,
      tableName: 'user',
      freezeTableName: true,
      timestamps: false
      );

      associate : (models) =>
      User.hasMany(models.Code,
      foreignKey: 'createdBy'
      );

      User.belongsTo(models.Role,
      foreignKey: 'role'
      );
      ;

      module.exports = User;


      Following is my code.js file -



      const Sequelize = require('sequelize');
      const db = require('./db');
      const User = require('./user');
      //one-to-many
      const Code = db.define('code',
      id:
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
      ,
      name:
      type: Sequelize.STRING
      ,
      // createdBy:
      // type: Sequelize.INTEGER
      // ,
      createdBy:
      type: Sequelize.INTEGER,
      references:
      // This is a reference to another model
      model: User,

      // This is the column name of the referenced model
      key: 'id'


      ,
      tableName: 'code',
      freezeTableName: true,
      timestamps: false
      );

      associate : (models) =>
      Code.belongsTo(models.User,
      foreignKey: 'createdBy'
      );
      ;

      module.exports = Code;


      When I run the app.js file I can't see the model reference of User,

      But I get the usual Integer value, Can someone please help on how to properly use the Model here?



      Error trace -
      enter image description here







      mysql node.js sequelize.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 14:29







      Aniruddha Raje

















      asked Mar 25 at 10:25









      Aniruddha RajeAniruddha Raje

      7711 gold badge16 silver badges44 bronze badges




      7711 gold badge16 silver badges44 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Looks like you're trying to fetch Code when connection wasn't established yet.



          Try this:



          const db = require('./db');
          const Code = require('./code');
          const User = require('./user');
          const Role = require('./role');

          function run()
          return db
          .authenticate()
          .then(() => Code.findOne( // you can try execute whenever query you want here
          where:
          id: 1
          )
          .then(code =>
          console.log("All users:", JSON.stringify(code, null, 4));
          )
          .catch(err => console.log("Error => n",err)))
          .catch(err =>
          console.error('Unable to connect to the database:', err);
          );


          run();





          share|improve this answer























          • tried async, updated question, didn't work.

            – Aniruddha Raje
            Mar 25 at 14:13











          • updated question :'(

            – Aniruddha Raje
            Mar 25 at 14:43













          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55335688%2fhow-to-use-sequelize-to-fetch-object-data%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Looks like you're trying to fetch Code when connection wasn't established yet.



          Try this:



          const db = require('./db');
          const Code = require('./code');
          const User = require('./user');
          const Role = require('./role');

          function run()
          return db
          .authenticate()
          .then(() => Code.findOne( // you can try execute whenever query you want here
          where:
          id: 1
          )
          .then(code =>
          console.log("All users:", JSON.stringify(code, null, 4));
          )
          .catch(err => console.log("Error => n",err)))
          .catch(err =>
          console.error('Unable to connect to the database:', err);
          );


          run();





          share|improve this answer























          • tried async, updated question, didn't work.

            – Aniruddha Raje
            Mar 25 at 14:13











          • updated question :'(

            – Aniruddha Raje
            Mar 25 at 14:43















          0














          Looks like you're trying to fetch Code when connection wasn't established yet.



          Try this:



          const db = require('./db');
          const Code = require('./code');
          const User = require('./user');
          const Role = require('./role');

          function run()
          return db
          .authenticate()
          .then(() => Code.findOne( // you can try execute whenever query you want here
          where:
          id: 1
          )
          .then(code =>
          console.log("All users:", JSON.stringify(code, null, 4));
          )
          .catch(err => console.log("Error => n",err)))
          .catch(err =>
          console.error('Unable to connect to the database:', err);
          );


          run();





          share|improve this answer























          • tried async, updated question, didn't work.

            – Aniruddha Raje
            Mar 25 at 14:13











          • updated question :'(

            – Aniruddha Raje
            Mar 25 at 14:43













          0












          0








          0







          Looks like you're trying to fetch Code when connection wasn't established yet.



          Try this:



          const db = require('./db');
          const Code = require('./code');
          const User = require('./user');
          const Role = require('./role');

          function run()
          return db
          .authenticate()
          .then(() => Code.findOne( // you can try execute whenever query you want here
          where:
          id: 1
          )
          .then(code =>
          console.log("All users:", JSON.stringify(code, null, 4));
          )
          .catch(err => console.log("Error => n",err)))
          .catch(err =>
          console.error('Unable to connect to the database:', err);
          );


          run();





          share|improve this answer













          Looks like you're trying to fetch Code when connection wasn't established yet.



          Try this:



          const db = require('./db');
          const Code = require('./code');
          const User = require('./user');
          const Role = require('./role');

          function run()
          return db
          .authenticate()
          .then(() => Code.findOne( // you can try execute whenever query you want here
          where:
          id: 1
          )
          .then(code =>
          console.log("All users:", JSON.stringify(code, null, 4));
          )
          .catch(err => console.log("Error => n",err)))
          .catch(err =>
          console.error('Unable to connect to the database:', err);
          );


          run();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 11:04









          Eugene ShilinEugene Shilin

          1515 bronze badges




          1515 bronze badges












          • tried async, updated question, didn't work.

            – Aniruddha Raje
            Mar 25 at 14:13











          • updated question :'(

            – Aniruddha Raje
            Mar 25 at 14:43

















          • tried async, updated question, didn't work.

            – Aniruddha Raje
            Mar 25 at 14:13











          • updated question :'(

            – Aniruddha Raje
            Mar 25 at 14:43
















          tried async, updated question, didn't work.

          – Aniruddha Raje
          Mar 25 at 14:13





          tried async, updated question, didn't work.

          – Aniruddha Raje
          Mar 25 at 14:13













          updated question :'(

          – Aniruddha Raje
          Mar 25 at 14:43





          updated question :'(

          – Aniruddha Raje
          Mar 25 at 14:43



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55335688%2fhow-to-use-sequelize-to-fetch-object-data%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript