亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何從提取調用 EXpress NoseJs Javascript 中解析此結果

我如何從提取調用 EXpress NoseJs Javascript 中解析此結果

慕容708150 2022-12-02 16:25:15
這是我的腳本代碼,它在 index.html 文件中,我知道放在那里是錯誤的,但首先我會嘗試讓它工作,然后我會移動它。readOperaciones();        async function readOperaciones(){            try{                const listaOr = document.getElementById('listaOrdenada');                const result = await fetch("http://localhost:8080/operaciones", {method: "GET"})                const operaciones = await JSON.parse(result)                //operaciones.forEach(t=>{                    for (var i = 0; i < operaciones.length; i++) {                    var row = operaciones[i];                    console.log(row.codeemp);}                    /*tt = JSON.stringify(t);                    const li = document.createElement("li");                    li.textContent = tt.text;*/                    /*t.forEach(cell=>{                        const li = document.createElement("li")                        li.textContent = cell.text;                        li.id = cell.id;                    })*/                //})            }            catch(e){                console.log("Error al leer las operaciones descriptas")            }        }這是與快遞的連接const {Client} = require('pg');const express = require ("express")const app = express();app.use(express.json())const client = new Client({  user: "postgres",  password: "1234",  host: "localhost",  port: 5432,  database: "webaduana",})app.get("/", (req, res) => res.sendFile(`${__dirname}/index.html`))app.get("/operaciones", async (req, res) => {    const rows = await readAll();    res.setHeader("content-type", "application/json")    res.send(JSON.stringify(rows))})async function readAll(){    try{        const results = await client.query("select * from operaciones")        return results.rows;    }    catch(e){        console.log(e)        return [];    }}我不知道我是否需要提供更多信息,但我對所有這些代碼的問題都在這里我已經嘗試了很多方法,但我無法在 ol 元素中獲得這些結果。它沒有給我任何錯誤,它只是不在 HTML 頁面中打印任何內容
查看完整描述

3 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

你可以使用 .json() 方法:


const fetched = await fetch("/url", {

  method: 'GET',

});


const fetchedJson: object = await fetched.json();


console.log(fetchedJson)


查看完整回答
反對 回復 2022-12-02
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

有幾種方法可以做到這一點。


使用承諾

獲取響應對象本身包含一些方法,可以幫助您獲得不同形式的響應,例如.json()、.text()和.status。點擊此處了解詳情。所以,如果你只是想將答案解析成一個 JSON 對象,你可以這樣做


function doSomethingOnParsedJson(res) {

  // Do something on the response here...

}


function readOperacions() {

  fetch("http://localhost:8080/operaciones", {

    method: "GET",

  })

     .then(res => res.json())

     .then(doSomethingOnParsedJson) // Pass in a function without parentheses

     .catch(console.error);

}

如果你定義一個單獨的函數來執行你想對解析的響應做的工作并將函數(不帶括號)傳遞給它會更干凈,then但你也可以繼續并直接給它一個函數,如:


function readOperacions() {

  fetch("http://localhost:8080/operaciones", {

    method: "GET",

  })

     .then(res => res.json())

     .then(parsedResponse => {

       // do something...

     })

     .catch(console.error);

}

使用異步/等待

您還可以使用異步/等待功能來實現這一點。


function doSomethingOnParsedJson(res) {

  // Do something on the response here...

}


async function readOperacions() {

  try {

    // Get the response from the server.

    const res = await fetch("http://localhost:8080/operaciones", {

      method: "GET",

    });

  

    // Parse it into a JSON object.

    const parsedJson = res.json();


    // Do something on it.

    doSomethingOnParsedJson(parsedJson);

  } catch (error) {

    // Show an error if something unexpected happened.

  }

}

邊注

在 Express 中有一種更簡潔的方法來發送 JSON 響應。您可以.json在 Express 響應對象上使用該方法。


app.get("/operaciones", async (req, res) => {

  const rows = await readAll();


  /* Don't do this!

  res.setHeader("content-type", "application/json")

  res.send(JSON.stringify(rows))

  */


  /* Do this instead ;-) */

  res.json(rows);

})

瞧!就這么簡單。


查看完整回答
反對 回復 2022-12-02
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

將 .then 添加到獲取鏈并打印結果:


fetch('http://example.com/movies.json')

  .then(response => {

    console.log('response: ' + JSON.stringify(response));

  })

  ...

  ...

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch


查看完整回答
反對 回復 2022-12-02
  • 3 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號