【问题标题】:ReactJs And Firestore: Properties of mapped objects are undefinedReactJs 和 Firestore:映射对象的属性未定义
【发布时间】:2020-04-02 19:20:30
【问题描述】:

我正在尝试在我的 SignUp 类中创建用户名列表,以仅允许创建唯一的显示名。但是,当尝试如下查看对象的 displayName 时,在控制台中,当初始对象被映射时,我可以看到 displayName,但尝试将其存储为变量会给我的值未定义。

function nameCheck(){
    let userList = [];
    db.collection("userNames").get().then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data()) //I can see the parameter displayName here with //proper value
          console.log(doc.displayName) //here it is being logged as undefined 
        });
        console.log(userList); //I can see the array has the right amount of objects, but they are all //undefined 


    });

我确信这很简单,但我根本无法让它工作。

【问题讨论】:

    标签: javascript reactjs google-cloud-firestore


    【解决方案1】:

    我想你的意思是:

    console.log(doc.data().displayName)
    

    文档属性的值在data()返回的对象中。

    【讨论】:

    • 在堆栈溢出时,习惯上对有帮助的答案进行投票并标记为正确。
    猜你喜欢
    • 2021-08-12
    • 2017-03-31
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多