您如何插入“参考"值入消防局? [英] How do you insert a "reference" value into firestore?

查看:65
本文介绍了您如何插入“参考"值入消防局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文档插入集合中.我希望文档具有类型为reference的属性以插入到集合中.但是每次我插入到集合中时,它都会以字符串或对象的形式出现.如何以编程方式插入reference类型的值?

I'm trying to insert a document into a collection. I want the document to have a attribute of type reference to insert into the collection. But every time I insert into the collection, it comes out as a string or an object. How can I programmatically insert a reference typed value?

绝对有可能在用户界面中做到这一点:

It's definitely possible to do it in the UI:

推荐答案

可能最简单的解决方案是将引用键的值设置为doc(collection/doc_key).ref,因为需要DocumentReference.

Probably the simplest solution is to set the value of a reference key to a doc(collection/doc_key).ref because a DocumentReference is needed.

示例代码:

post = {
  content: "content...",
  title: "impressive title",
  user: db.doc('users/' + user_key).ref,
};

db.collection('posts').add(post)

这篇关于您如何插入“参考"值入消防局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆