javascript 未处理的错误类型错误:无法读取未定义的属性

14ifxucb  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(191)

我一整天都在拼命想让它工作。
我有一个对象,我试图在键值上循环该对象类型为Map〈String,Array〉,从iOS应用程序发送到我的firebase函数。
我尝试循环键值并看到一个错误。奇怪的是记录值打印正确的值但赋值不起作用

export const createBooking = functions.https.onCall(async (data, context) => {
    if (!context.auth || !context.auth.uid) {
        throw new functions.https.HttpsError('unauthenticated', 'User not authed');
    }

    const groomerId: string = data.groomerId;
    const unixTimestamp: number = data.date;
    const requestedServicesDict: Map<string, Array<string>> = data.requestedServices;
    const customerId = context.auth.uid;

    const groomer = await admin.firestore()
        .doc('groomers/' + groomerId)
        .get();

    const customer = await admin.firestore()
        .doc('users/' + customerId)
        .get();

    const allServices = await admin.firestore()
        .collection('services')
        .get();

    const serviceFee: number = 5
    let servicesTotalPrice = 0
    let servicesTotalDuration = 0
    let requestedServices: Array<any> = []

    console.debug(requestedServicesDict)
    console.debug(typeof(requestedServicesDict))

    for (const [petId, serviceIdsArray] of Object.entries(requestedServicesDict)) {
        console.debug('petid')
        console.debug(petId)
        console.debug(typeof(serviceIdsArray))

        console.debug(serviceIdsArray)
        const serviceIds: Array<string> = serviceIdsArray // L42

        const pet = await admin.firestore()
            .doc(`pets/${petId}`) // // L42
            .get();

        const petWeight: number = pet.data()?.weight

        const servicesAndPricing = serviceIds.map(id => {
            const service = allServices.docs.find(item => item.id == id)
            const pricingAndDuration = groomer.data()?.servicePriceAndDuration[id]
            let price: number
            let duration: number

            if (service?.data().pricedOnPetSize == true) {
                if (petWeight <= 10) {
                    price = pricingAndDuration.extraSmallPrice
                    duration = pricingAndDuration.extraSmallDuration
                } else if (petWeight > 10 && petWeight <= 20) {
                    price = pricingAndDuration.smallPrice
                    duration = pricingAndDuration.smallDuration
                } else if (petWeight > 20 && petWeight <= 50) {
                    price = pricingAndDuration.mediumPrice
                    duration = pricingAndDuration.mediumDuration
                } else if (petWeight > 50 && petWeight <= 100) {
                    price = pricingAndDuration.largePrice
                    duration = pricingAndDuration.largeDuration
                } else {
                    price = pricingAndDuration.extraLargePrice
                    duration = pricingAndDuration.extraLargeDuration
                }
            } else {
                price = pricingAndDuration.price
                duration = pricingAndDuration.duration
            }

            servicesTotalPrice += price
            servicesTotalDuration += duration

            requestedServices.push({
                service: service,
                price: price
            })
        })

        requestedServices.push({
            pet: pet.data(),
            servicesAndPricing: servicesAndPricing
        })
    }

这是日志

DEFAULT 2023-01-06T23:57:40.404119Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] {
DEFAULT 2023-01-06T23:57:40.404136Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] NtQBkoN9xyfyyDisq51P: [ 'xTXpy3NbIF83rQoE4C2b', '1zveuGiOZ8o55YHyYICB' ]
DEFAULT 2023-01-06T23:57:40.404143Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] }
DEFAULT 2023-01-06T23:57:40.404187Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] petid
DEFAULT 2023-01-06T23:57:40.404240Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] NtQBkoN9xyfyyDisq51P
DEFAULT 2023-01-06T23:57:40.404271Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] object
DEFAULT 2023-01-06T23:57:40.404360Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] [ 'xTXpy3NbIF83rQoE4C2b', '1zveuGiOZ8o55YHyYICB' ]
ERROR 2023-01-06T23:57:40.496526Z [resource.labels.functionName: createBooking] [labels.executionId: xdalgmf91s6u] Unhandled error TypeError: Cannot read properties of undefined at /workspace/lib/bookings.js:45:125 at Array.map (<anonymous>) at /workspace/lib/bookings.js:42:47 at processTicksAndRejections (node:internal/process/task_queues:96:5) at async /workspace/node_modules/firebase-functions/lib/common/providers/https.js:407:26
  {
    "textPayload": "Unhandled error TypeError: Cannot read properties of undefined\n    at /workspace/lib/bookings.js:45:125\n    at Array.map (<anonymous>)\n    at /workspace/lib/bookings.js:42:47\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async /workspace/node_modules/firebase-

编辑:调用函数的Swift代码

typealias PetId = String
typealias ServiceId = String
typealias RequestedServicesDictionary = [PetId: [ServiceId]]

func createBooking(groomerId: String, date: Date, requestedServices: RequestedServicesDictionary) async throws {
    _ = try await Functions.functions().httpsCallable("createBooking").call([
        "groomerId": groomerId,
        "date": date.timeIntervalSince1970,
        "requestedServices": requestedServices
    ])
}
ev7lccsx

ev7lccsx1#

我认为问题出在第42行:

const serviceIds: Array<string> = Array(serviceIdsArray)

serviceIdsArray已经是一个数组了,这样就变成了数组的数组了,除非这就是你想要的,Typescript不是说输入有错误吗?

相关问题