Firebase云功能检查db是否存在不存在的数据 [英] Firebase cloud functions check db for non-existant data

查看:47
本文介绍了Firebase云功能检查db是否存在不存在的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何检查我的云功能中是否存在文档 当增加一个现有值时,我的belows功能可以正常工作,但是现在我尝试在其中添加功能,以检查以前的值是否存在以及是否未设置为1.

I'm looking for how to check if a documents exists in my cloud functions My functions belows works fine when just incrementing an existing value, but now I'm trying to add functionality where it checks to see if the previous value exists and if it doesn't set as 1.

我尝试了不同的方法,但出现类似"snapshot.exists"或"TypeError:无法读取docRef.get.then.snapshot中未定义的属性'count'

I've tried a different methods but I get things like "snapshot.exists" or "TypeError: Cannot read property 'count' of undefined at docRef.get.then.snapshot

var getDoc = docRef.get()
        .then(snapshot => {

            if (typeof snapshot._fieldsProto.count !== undefined) {
                console.log("haha3", snapshot._fieldsProto.count)

                var count = Number(jsonParser(snapshot._fieldsProto.count, "integerValue"));

                docRef.set({
                        count: count + 1
                    });
            }
            else {
                docRef.set({
                        count: 1
                    });
            }

        });

下面是exist()错误的代码

below is the code for the exists() error

var getDoc = docRef.get()
        .then(snapshot => {

            if snapshot.exists() {
                console.log("haha3", snapshot._fieldsProto.count)

                var count = Number(jsonParser(snapshot._fieldsProto.count, "integerValue"));

                docRef.set({
                        count: count + 1
                    });
            }
            else {
                docRef.set({
                        count: 1
                    });
            }

        });

此代码的错误是:

TypeError:snapshot.exists不是docRef.get.then.snapshot中的函数

TypeError: snapshot.exists is not a function at docRef.get.then.snapshot

推荐答案

似乎docRef要么指向一个集合,要么是一个查询.在这种情况下,您的snapshot类型为QuerySnapshot.

It seems like docRef either points to a collection or is a query. In that case your snapshot is of type QuerySnapshot.

要检查查询是否有结果,请使用 QuerySnapshot.empty .

To check if a query has any result, use QuerySnapshot.empty.

这篇关于Firebase云功能检查db是否存在不存在的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆