T
T
TCloud2019-05-08 04:22:53
Vue.js
TCloud, 2019-05-08 04:22:53

How to work with the database from Vue components?

Actually the question is clear.
There is a single-file VueJS component and a PostgreSQL database.
Deployed the server, wrote the following script:

'use strict';

const { Pool } = require('pg');

const pool = new Pool({
  host: 'localhost',
  port: 5432,
  database: 'application',
  user: 'sysadmin',
  password: 'password123'
});

(async () => {
  const fields = ["tablename", "tableowner"].join(', ');
  const sql = `SELECT * FROM pg_tables`;
  const { rows } = await pool.query(sql);
  console.table(rows);
  pool.end();
})();

How to return the received data to the Vue component from this script?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Terentiev, 2019-05-08
@OTCloud

To interact, you need to write rest-full methods, or use another of the popular technologies, such as GraphQL.
You can try nodejs framework express for this purpose.
You need to implement the route on the server, example:

app.get('/get-data', function (req, res) {
 // запрос к базе
// нормализовать данные в нужный вам вид (json).
  res.send(данные);
});
// по маршруту /get-data вы получите данные из базы

https://expressjs.com/ru/guide/routing.html

R
Robur, 2019-05-08
@Robur

expand now also api. For example, on express - the easiest way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question