最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Node.js Firestore身份验证问题(在javascript正常运行时]

网站源码admin24浏览0评论

Node.js Firestore身份验证问题(在javascript正常运行时]

Node.js Firestore身份验证问题(在javascript正常运行时]

我希望匿名连接到Firestore并从集合中获取一些数据。在javascript下,此方法运行良好,但在node.js下却失败(“ FirebaseError:缺少权限或权限不足”)。任何指针将不胜感激。

这是works在javascript下没有任何干扰的代码(我省略了'script'包括在内,并且它按预期返回数据:

var config = {
  apiKey: "xxx",
  authDomain: "xxx.firebaseio",
  projectId: "xxx",
  storageBucket: "xxx.appspot",
};
firebase.initializeApp(config);

firebase.auth().signInAnonymously().catch(function(error) {
  var errorCode = error.code;
  var errorMessage = error.message;
  console.log(error.message);
});

foo();
async function foo() {
  var db=firebase.firestore();
  var query = await db.collection("collection").limit(5).get();
  query.forEach(function(doc) {
    console.log(doc.data());
  });
}

这是在node.js下执行not的代码。 config / authdata与上面的js示例完全相同。 (它使用firebase(客户端)库。我的理解是firebase-admin库不允许匿名登录。)

const firebase=require('firebase');

  var config = {
  apiKey: "xxx",
  authDomain: "xxx.firebaseio",
  projectId: "xxx",
  storageBucket: "xxx.appspot",
  };

firebase.initializeApp(config);

firebase.auth().signInAnonymously().catch(function(error) {
    var errorCode = error.code;
    var errorMessage = error.message;
    console.log(error.message);
  });

let db = firebase.firestore();

db.collection('collection').get()
  .then((snapshot) => {
    snapshot.forEach((doc) => {
      console.log(doc.id, '=>', doc.data());
    });
  })
  .catch((err) => {
    console.log('Error getting documents', err);
  });

当调用db.collection('collection')。get()时触发以下错误。 (之前的匿名签名通过)

Error getting documents { FirebaseError: Missing or insufficient permissions.
    at new FirestoreError (/root/node_modules/@firebase/firestore/dist/index.node.cjs.js:1201:28)
    at JsonProtoSerializer.fromRpcStatus (/root/node_modules/@firebase/firestore/dist/index.node.cjs.js:19708:16)
    at JsonProtoSerializer.fromWatchChange (/root/node_modules/@firebase/firestore/dist/index.node.cjs.js:19955:44)
    at PersistentListenStream.onMessage (/root/node_modules/@firebase/firestore/dist/index.node.cjs.js:16828:43)
    at /root/node_modules/@firebase/firestore/dist/index.node.cjs.js:16757:30
    at /root/node_modules/@firebase/firestore/dist/index.node.cjs.js:16797:28
    at /root/node_modules/@firebase/firestore/dist/index.node.cjs.js:17844:20
    at process._tickCallback (internal/process/next_tick.js:68:7)
  code: 'permission-denied',
  name: 'FirebaseError',
  toString: [Function] }

再次感谢您提供指针!

回答如下:

一种可能性是,您在获取Firestore时实际上并未登录。事实上,signInAnonymously()方法isynchronous,您不必等待此方法返回的Promise在获取Firestore之前就已解析。

因此,以下可能解决您的问题:

signInAnonymously()

请注意,您应该在JavaScript SDK版本中执行相同的操作:

firebase.initializeApp(config);
let db = firebase.firestore();

firebase.auth().signInAnonymously()
.then(cred => {
  return db.collection('collection').get()
})
.then((snapshot) => {
    snapshot.forEach((doc) => {
      console.log(doc.id, '=>', doc.data());
    });
  })
.catch((err) => {
    console.log(err);
});
发布评论

评论列表(0)

  1. 暂无评论