簡體   English   中英

獲取 API 給我 undefined

[英]Fetch API gives me undefined

我是 nodejs 和 API 的新手,但我正在嘗試將來自 API 的數據顯示到瀏覽器,但它顯示為未定義。 當我檢查頁面時,數據在網絡下。 我怎樣才能解決這個問題?

我的路由器:


array=[];
Router.get('/products', (req, res) => {
    res.send(array);
});

//get one product
Router.get('/:id', (req, res) => {
    mysqlConnection.query("SELECT * FROM products WHERE id = ?", [req.params.id], (err, rows, fields) => {
        if(!err){
           
          array.push(rows);
          res.send(rows);
          
        }  else{
            console.log(err);
        }
    });
});

在您的代碼行中是一個數組。 您將數組推送到數組。 你有一個二維數組。 您想將項目推送到數組,因此它是一維的

dataid.push(...rows);
res.send(rows);

您的路由器具有/productsIDD路徑,但您正在請求/products/productsIDD

請為/products/productsIDD編寫新路徑或將現有路徑 ( /productsIDD ) 重寫為/products/productsIDD

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM