typescript - Firebase 云函数 typescript 错误 "Not all code paths return a value"

标签 typescript google-cloud-firestore google-cloud-functions tslint

我正在使用 firebase 云功能和 firestore 交易来根据购买减少产品的可用数量。在部署时,它返回错误“错误 TS7030:并非所有代码路径都返回一个值”

这是代码

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';


admin.initializeApp();
const db = admin.firestore()

exports.newOrder = functions.firestore
.document('orders/{orderId}')
.onCreate(async (snap, context) => {

    try {
        const data = snap.data();
        if (data === undefined) return null

        const itemList = data.list as Array<any>
        const productId: Set<string> = new Set<string>();

        itemList.forEach((item) => {
            productId.add(item.id)
        })
        return db.runTransaction(async t => {

            const promises: Promise<admin.firestore.DocumentSnapshot>[] = []
            productId.forEach(i => {
                const p = admin.firestore().doc('Products/' + i).get()
                promises.push(p)
            })
            const docs=await Promise.all(promises)
            docs.forEach(doc => {
                if (!doc.exists) {
                    return Promise.reject("Product deleted")
                }
            })
            itemList.forEach(j => {
                if (j.variation === '-') {
                    const available = docs[j.id].get('available')
                    const needed = j.quantity
                    if (available < needed) {
                        return Promise.reject("Product out of stock")
                    }
                }
                else {
                    const variations = docs[j.id].get('variation') as Map<string, any>
                    for (const i in variations.keys) {
                        if (i === j.variation) {
                            const needed = j.quantity
                            const available = docs[j.id].get('variation').get(i).get('quantity')
                            if (available < needed) {
                                return Promise.reject("Product out of stock")
                            }
                        }
                    }
                }
            })
            itemList.forEach(j => {
                if (j.variation === '-') {
                    const available = docs[j.id].get('available')
                    const needed = j.quantity
                    t.update(db.doc('Products/' + j.id), { 'available': available - needed })
                }
                else {
                    const variations = docs[j.id].get('variation') as Map<string, any>
                    for (const i in variations.keys) {
                        if (i === j.variation) {
                            const needed = j.quantity
                            const available = docs[j.id].get('variation').get(i).get('quantity')
                            t.update(db.doc('Products/' + j.id), { [`variation.${i}.quantity`]: available - needed })
                        }
                    }
                }
            })
            return Promise.resolve("Product quantity updated")
        })
    }
    catch (error) {
        console.log(`error ${error}`)
        return null
    }
});

这是部署中显示的错误
 src/index.ts:30:30 - error TS7030: Not all code paths return a value.

 30                 docs.forEach(doc => {
                            ~~~~~~~~

 src/index.ts:35:34 - error TS7030: Not all code paths return a value.

 35                 itemList.forEach(j => {
                                ~~~~~~


 Found 2 error 

如何解决错误。

错误中提到的2个循环检查产品是否被删除以及产品是否缺货。如果它满足我想退出函数的条件。请帮我。

最佳答案

错误消息告诉您有些函数不会在所有情况下都返回值。它甚至会告诉哪些功能违反了这一要求。这是第一个错误:

 src/index.ts:30:30 - error TS7030: Not all code paths return a value.

 30                 docs.forEach(doc => {
                            ~~~~~~~~

它告诉您传递给 forEach 的函数有问题。

这是函数:
        docs.forEach(doc => {
            if (!doc.exists) {
                return Promise.reject("Product deleted")
            }
        })

请注意,当 doc.exists 时,该函数不会返回值。是真的。如果您不在乎这种情况,只需返回 null:
        docs.forEach(doc => {
            if (!doc.exists) {
                return Promise.reject("Product deleted")
            }
            else {
                return null
            }
        })

现在错误消失了,因为所有代码路径都返回一个值。您可以将相同的逻辑应用于另一个错误。

关于typescript - Firebase 云函数 typescript 错误 "Not all code paths return a value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976561/

相关文章:

firebase - 在 Firestore 上设置数据时将字符串转换为映射

javascript - 您可以创建动态 Firebase 查询吗?

node.js - Firebase 云函数本地代码更改未反射(reflect)在模拟器中

javascript - "Uncaught TypeError: Array.removeAt() is not a function",

angular - 错误 : No provider for NavParams

javascript - Angular Firestore |如何获取不包含的数组

linux - 尝试部署 Firebase 功能时找不到模块 'firebase-admin'

javascript - 单击元素时 Angular 关注输入

reactjs - 如何使用 React 和 TypeScript 创建通用箭头函数

go - 将多个文件传递给存储桶中的 exec.Command 调用