我正在尝试将新文档发布到包含对现有文档的引用的集合中。
这是我的代码:
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'
});
video
和user
引用其他集合中的现有文档,但是当我运行此代码时,字段存储为字符串而不是引用。 The documentation notably does not describe how to do this
如何将这些值作为参考发布?
答案 0 :(得分:6)
想出来 - 使用" 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')
});