【问题标题】:Create a document in Firestore that includes a reference to an existing document in another collection在 Firestore 中创建一个文档,其中包含对另一个集合中现有文档的引用
【发布时间】:2018-05-26 17:11:40
【问题描述】:

我正在尝试将新文档发布到包含对现有文档的引用的集合中。

这是我的代码:

var admin = require('firebase-admin');
var serviceAccount = require('./serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://DBURL.firebaseio.com'
});

var db = admin.firestore();

db.collection('myFunCollection').add({
  someBool: false,
  someNumber: 13.2,
  video: 'videos/Izdm35CsW6kqv2UxZEX0',
  user: 'users/pb7La4kzEaBow4iWvmxZ'
});

videouser 引用其他集合中的现有文档,但是当我运行此代码时,字段存储为字符串而不是引用。 The documentation notably does not describe how to do this.

如何将这些值作为参考发布?

【问题讨论】:

    标签: node.js firebase google-cloud-firestore


    【解决方案1】:

    想通了——通过使用“db.doc('{path}')”

    var admin = require('firebase-admin');
    var serviceAccount = require('./serviceAccountKey.json');
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: 'https://DBURL.firebaseio.com'
    });
    
    var db = admin.firestore(); 
    
    db.collection('myFunCollection').add({
      someBool: false,
      someNumber: 13.2,
      video: db.doc('videos/Izdm35CsW6kqv2UxZEX0'),
      user: db.doc('users/pb7La4kzEaBow4iWvmxZ')
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 2013-07-28
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2014-08-19
      相关资源
      最近更新 更多