简体   繁体   English

NativeScript-Vue Firebase + FireStore 使用

[英]NativeScript-Vue Firebase + FireStore use

Im trying to use FireStore for my NativeScript-Vue android app.我试图将 FireStore 用于我的 NativeScript-Vue android 应用程序。 FireBase initialization is succesfull. FireBase 初始化成功。 Now I want to access to my collection which has one document.现在我想访问我的收藏,它有一个文档。 In my.Vue component tags i write this:在 my.Vue 组件标签中,我这样写:

import {firestore} from "@nativescript/firebase"

mounted() {
//fetch data from the firestore
firestore.collection('Husqvarnas').get()
.then(snapshot => {
  snapshot.forEach((doc) => {
    console.log(doc.data(), doc.id)
  })
})}

What im trying to do there is to console.log all the data, and Documents individually with 'doc.id' But i get nothing .我试图做的是控制台记录所有数据,并使用'doc.id'单独记录文档但我什么也没得到。 No errors, no results.没有错误,没有结果。 If I add bellow console.log this code:如果我在下面添加 console.log 这段代码:

let husky = doc.data();
    husky.id = doc.id;
    this.Husqvarnas.push(husky)

and trying to display it with this code:并尝试使用以下代码显示它:

<ListView for="husky in Husqvarnas" :key="husky.id"> <!-- :key="husky.Model" -->
    <v-template>
        <StackLayout>
            <Label :text="`Model: ${husky.Model}`" textWrap="true"/>
            <Label :text="`Front Compression: ${husky.FCompr}`" textWrap="true"/>
        </StackLayout>
    </v-template>
</ListView>

I get " Cannot read property 'id' of undefined ".我得到“无法读取未定义的属性 'id' ”。 Here i am Initializing firebase:这里我正在初始化 firebase:

var firebase = require("@nativescript/firebase").firebase;
firebase.init({
}).then(
    function () {
        console.log("firebase.init done");
    },
    function (error) {
        console.log("firebase.init error: " + error);
    }
);

And getting "firebase.init done", so its working.并完成“firebase.init”,这样它就可以工作了。

What i am doing WRONG?我做错了什么

Try this simple one:试试这个简单的:

mounted() {
    firestore.collection("Husqvarnas").get()
    .then(function(snapshot) {
        snapshot.forEach(function(doc) {
                console.log(doc.id, " => ", doc.data());
        });
    });
}

It was Security Rules, had to change in default rules from False, to True.这是安全规则,必须将默认规则从 False 更改为 True。

    rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM