簡體   English   中英

POST 請求正文中的數組不可迭代

[英]Array in POST request body not iterable

我正在為我的網站構建一個私有 API,並試圖攝取通過 API 端點傳遞給程序的數據。 我試圖遍歷存儲在一個鍵中的數組中的所有項目。 向 API 端點發出的 POST 請求的主體形狀如下:

{
    "tracked": [
        {
            "categoryId": 100,
            "categoryName": "Cars",
            "items": [
                {
                    "item": "Red Car",
                    "itemId": "",
                    "limit": 1
                },
                {
                    "item": "Blue Car",
                    "itemId": "",
                    "limit": 1
                },
            ]
        },
        {
            "categoryId": 200,
            "categoryName": "Trucks",
            "items": [
                {
                    "item": "Red Truck",
                    "itemId": "",
                    "limit": 1
                },
                {
                    "item": "Blue Truck",
                    "itemId": "",
                    "limit": 1
                },
            ]
        }
    ]
}

我的問題是:當嘗試在“tracked”鍵處遍歷數組時,我的應用程序拋出異常“TypeError: tracked.tracked is not iterable” 。為什么會這樣,我將如何解決這個問題? tracked.tracked的值顯然是一個數組。下面是不起作用的代碼。

import * as functions from "firebase-functions";
import * as express from 'express'

// init express app
const app = express()

// my routings
const apiRoute = require("./api")

// enable json parsing
app.use(express.json())

// add routes to the express app.
app.use("/api", apiRoute)

exports.api = functions.https.onRequest(app)
router.route('/data/tracked').post( async (req, res) => {
    
    const tracked = req.body.tracked as Tracked
    const response = postTracked(tracked)
    res.status(200).send(response)

})
/**
 * Ingests a json of tracked items and categorys formatted as type Tracked
 * @param tracked json of categorys and their respective items being tracked
 * @returns response with indicated success.
 */

export const postTracked = async (tracked: Tracked) => {

    // set item id's in ingested json.
    const newTracked: Tracked = {
        tracked: [],
    }
    for (const cat of tracked.tracked) {
        const category: Category = {
            categoryId: cat.categoryId,
            categoryName: cat.categoryName,
            items: []
        }
        for (const itm of cat.items) {
            const itemId = `${cat.categoryId}-${generateRandomString(5)}`
            category.items.push({
                item: itm.item,
                itemId: itemId,
                limit: itm.limit,  
            } as Item)
        }
        newTracked.tracked.push(category);
        
    }

    // update the tracked list.
    await db.collection('trackers').doc('eBay').set(tracked, {merge: true})

    return responseBuilder(true, (await db.collection('trackers').doc('eBay').get()).data())
}

這個 function 以前在我使用 express 時有效,而只是firebase.functions.onCall如下所示。

const ingestTrackedJSON = functions.https.onCall(async (data, context) => {

    // set item id's in ingested json.
    const tracked: TrackedItems = {
        tracked: [],
    }
    for (const cat of ingest.tracked) {
        const category: Category = {
            categoryId: cat.categoryId,
            categoryName: cat.categoryName,
            items: []
        }
        for (const itm of cat.items) {
            const itemId = `${cat.categoryId}-${generateRandomString(5)}`
            category.items.push({
                item: itm.item,
                itemId: itemId,
                limit: itm.limit,  
            } as Item)
        }
        tracked.tracked.push(category);
        
    }

    // update the tracked list.
    db.collection('trackers').doc('eBay').set(tracked, {merge: true})

    return true;

您正在嘗試訪問“錯誤的” tracked 這就是我的意思:

export const postTracked = async (tracked: Tracked) => {

    // set item id's in ingested json.
    const newTracked: Tracked = {
        tracked: [],
    }
    for (const cat of tracked.tracked) { // <--- Here you should just use tracked

因為您已經傳遞了正文 object。正確的代碼行應該是

    for (const cat of tracked) {

暫無
暫無

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

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