【问题标题】:use result of data from database to App.js使用数据库中的数据结果到 App.js
【发布时间】:2021-02-17 20:12:27
【问题描述】:

到目前为止,我已经在 pg.js 上得到了这段代码......我想在我的 App.js 中使用 {rows} 的结果......我尝试了 evreything......我无法让它工作我需要帮忙。谢谢

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

const client = new Client({
 ...
    ssl: {
        rejectUnauthorized: false
    }
});



async function execute(){

    client.connect()
        .then(() => console.log("connected to server"))
    const {rows} = await  client.query('select * from ggl_trends')
    console.table(rows)

}

execute();

【问题讨论】:

    标签: node.js reactjs pg


    【解决方案1】:

    连接建立后,您必须发送查询。

    async function execute() {
      await client.connect()
      console.log('Connected to server')
      const { rows } = await client.query('select * from ggl_trends')
      console.table(rows)
    }
    

    【讨论】:

    • 我尝试放入一个 ... 类 ... extends React.Component {... 但我给了我很多问题 TypeError: net.Socket is not a constructor constructor(config) { 16 |超级() 17 |配置 = 配置 || {} > 18 | this.stream = config.stream ||新的 net.Socket() | ^ 19 | this._keepAlive = config.keepAlive 20 | this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis 21 | this.lastBuffer = false
    • @IgorSilva 不确定我是否理解。与数据库对话的代码应该在后端。
    • const {Client} = require('pg');使用 pg 异步函数 execute(){ client.connect() .then(() => console.log("connected to server")) const {rows} = await client.query('select * from ggl_trends') 控制台。 table(rows) } 这将数据获取到 const {rows} ...现在我想在我的 App.js 中显示它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多