小编Ada*_*dam的帖子

将文档添加到 Firestore 中不存在的子集合

希望这不是太明显,但在这一点上我真的很难过。我无法将文档添加到 Firebase Firestore 中不存在的子集合。

添加到已经存在的子集合时,该代码可以完美运行。

db.collection("collectionId").doc(docId).collection("subcollectionId").doc(subdocId).collection("items").add({
    userName,
    isActive: true        
}).then(function() {
    console.log("Document successfully written!");
}).catch(function(error) {
    console.error("Error writing document: ", error);
});
Run Code Online (Sandbox Code Playgroud)

在这里要清楚,子子集合“项目”存在于我的一些子集合中,但尚未在其他子集合中创建。当我尝试在已经有其他“项目”的子集合中使用上述代码将文档添加到“项目”时,它会产生以下结果:

Document successfully written!
Run Code Online (Sandbox Code Playgroud)

当我尝试将文档添加到“items”不存在的子集合中的“items”时,它不会添加该项目并且没有任何内容写入控制台。

我在这里先向您的帮助表示感谢!

根据要求更新屏幕截图:

成功: 在此处输入图片说明

失败: 在此处输入图片说明

javascript google-cloud-firestore

5
推荐指数
1
解决办法
564
查看次数

在时间戳字段上查询Firestore数据库

我对Firestore(和编程)比较陌生,无法在线解决我的问题。

寻找查询现有集合中的文档。所有文档都有一个时间戳字段。

当我尝试立即查询所有文档“>”或“ <”时,查询工作正常。7天前,当我尝试查询“>”或“ <”时,查询不返回任何内容。我确定我可能只是想念一些小东西。谢谢你的帮助!

这些退货文件符合预期:

var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
Run Code Online (Sandbox Code Playgroud)

var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '<', today).get().then(function(querySnapshot) {
Run Code Online (Sandbox Code Playgroud)

这些不返回任何内容:

var today = new Date()-604800000;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
Run Code Online (Sandbox Code Playgroud)

var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today-604800000).get().then(function(querySnapshot) {
Run Code Online (Sandbox Code Playgroud)

只是为了它

var today = new Date()-1;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
Run Code Online (Sandbox Code Playgroud)

我见过其他人要求查看Firestore中的字段的外观,所以这是一张图片: 抱歉,这是一个链接

请让我知道是否还有其他可能有用的信息。谢谢!

编辑以显示下一个尝试:

var config = {****};
firebase.initializeApp(config);

const db = firebase.firestore();
const settings …
Run Code Online (Sandbox Code Playgroud)

firebase google-cloud-firestore

2
推荐指数
1
解决办法
4129
查看次数

使用nodejs从firestore文档中删除字段

我尝试从 Firestore 文档中删除字段,但没有成功。

我已经搜索了收到的错误消息,但我可以找到的几页链接指向我已经在做的事情。我确信这很简单,但我现在不知所措。

const Firestore = require('@google-cloud/firestore');
const admin = require('firebase-admin');
const FieldValue = admin.firestore.FieldValue;

const firestore = new Firestore({
  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
});
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);


var updateDoc = firestore.collection('XXXXXXXX').doc('XXXXXXX').update({
    fieldToBeDeleted: FieldValue.delete()
}); 
Run Code Online (Sandbox Code Playgroud)

我希望删除该字段,但我收到以下错误消息:

Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Argument "dataOrField" is not a valid Document. Couldn't serialize object of type "DeleteTransform". Firestore doesn't support JavaScript objects with custom …
Run Code Online (Sandbox Code Playgroud)

javascript node.js firebase google-cloud-platform google-cloud-firestore

2
推荐指数
1
解决办法
1193
查看次数