M
M
microf2018-11-05 10:49:01
Node.js
microf, 2018-11-05 10:49:01

What does Sequelize give?

Good afternoon. Making a request to the database

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

through sequelize, I get a sheet in the console, including connecting to the database and so on. And without stopping
_evictionIterator:
                                      DequeIterator {
                                        _list:
                                         DoublyLinkedList {
                                           head:
                                            { prev: null,
                                              next:
                                               { prev: [Circular],
                                                 next: null,
                                                 data:
                                                  PooledResource {
                                                    creationTime: 1541403796957,
                                                    lastReturnTime: 1541403796967,
                                                    lastBorrowTime: 1541403796962,
                                                    lastIdleTime: 1541403796967,
                                                    obj:
                                                     Client {
                                                       domain: null,
                                                       _events:
                                                        [Object: null prototype] { end: [Function], error: [Function] },
                                                       _eventsCount: 2,
                                                       _maxListeners: undefined,
                                                       connectionParameters:
                                                        ConnectionParameters {
                                                          user: 'postgres',

And so on.
My code
import { Sequelize } from 'sequelize-typescript';
import * as express from 'express';
import { appconfig } from '../config/appconfig';
import app from "./app";
import {User  } from "../modules/user/models/user.model";


const server = express();
// allow access from client server
server.use(function (req, res, next) {

    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    next();
});



export const sequelize = new Sequelize({
    database: appconfig.dbdatabase,
    dialect: appconfig.dbdialect,
    username: appconfig.dbusername,
    password: appconfig.dbpassword,
    host: appconfig.dbhost,
    port: appconfig.dbport
});

sequelize.authenticate().then(() => {
    console.log("Connected to DB");
})
    .catch((err) => {
        console.log(err);
    })
app.listen(appconfig.port, () => {
    console.log('Express server listening on port ' + appconfig.port);
})
sequelize.addModels([User]);
User.findAll().then(users => {
    console.log(users)
  });

What does sequelize return? Is it because there is no promise?
I took an example request from Getting Started

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Gerasimov, 2018-11-06
@Omashu

Returns an object with its own iterator implementation, you can iterate over this object, each data stack is wrapped in a model in it. For output to the console, they can be brought into a readable form, for example, like this: https://learn.javascript.ru/iterator

A
Alexander Aladin, 2018-11-05
@sanchos86

You can pass an additional object with options to the findAll method, one of which allows you to get only data from the database: findAll .
In your case, the request will look like this:

User.findAll({ raw: true }).then(users => {
    console.log(users)
 });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question