Firestore查询功能结果无效

时间:2020-10-06 05:35:10

标签: javascript typescript

函数 validateBusinessId 导致变量 businessExists 无效。我正在尝试根据业务是否存在于数据库中或者上传的文件是否为主文件来分配不同的数据库路径。函数 validateBusiness 产生无效结果。不知道如何解决它。我哪里出错了?

async function validateBusinessId(businessId: string) {
    db.collection('business').doc(businessId).get()
    .then((docSnapshot) => {
        if (docSnapshot.exists) {
            return true
        } else {
            return false
        }
    })
}

async function getDatabase(fileName: string, businessId: string) {
    const businessExists = await validateBusinessId(businessId)

    if ((fileName) && (fileName?.includes('masterFile'))) {
        console.log('A new Master database file has been detected.')
        return db.collection('products')
    } else if ((businessId) && (businessExists)) {
        // If the business exists, return business path
        console.log('A new database file has been detected for business: ' + businessId)
        return db.collection('business').doc(businessId).collection('products')
    } else {
        return null
    }
}

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好,所以我发现我对查询结果的承诺处理不正确。这是对我有用的代码。

async function validateBusinessId(businessId: string) {
    const databaseDocumentSnapshot = await db.collection('business').doc(businessId).get()

    if (databaseDocumentSnapshot.exists) {
        return true
    } else {
        return false
    }
}

async function getDatabase(fileName: string, businessId: string) {
    const businessExists = await validateBusinessId(businessId)

    if ((fileName) && (fileName?.includes('masterFile'))) {
        console.log('A new Master database file has been detected.')
        return db.collection('products')
    } else if ((businessId) && (businessExists)) {
        return db.collection('business').doc(businessId).collection('products')
    } else {
        console.log('No business with such ID exists: ' + businessId);
        return null
    }
}