gpt4 book ai didi

Firebase Firestore 时间戳(如何实现)

转载 作者:行者123 更新时间:2023-12-02 03:27:25 26 4
gpt4 key购买 nike

在尝试记录提交时间(以便我稍后可以通过它进行过滤)提交到 firebase 时遇到问题。

让我展示一下我现在拥有的(工作状态)

--

目前,我有一个表单,用户可以在其中提交一些信息,并将其推送到 firebase。除了我需要的自动时间戳之外,一切正常(来自手动输入)。

export default {
name: 'SubmitResource',

data() {
return {
resourceTitle: null,
featuredImage: null,
creatorsName: null,
creatorProfile: null,
creatorImage: null,
resourceOriginalLink: null,
resourceCategory: null,
resourcePrice: null,
downloadLink: null,
resourceDescription: null,
date: null,
slug: null,
}
},

methods: {
SubmitResource(){
if (this.resourceTitle){
this.feedback = null
//create slug
this.slug = slugify(this.resourceTitle, {
replacement: '-',
remove: /[$*_+-:@"'`~±§?()]/g,
lower: true
})

if (this.resourcePrice == 0) {
this.resourcePrice = "FREE"
}


db.collection('resources').add({

resourceTitle: this.resourceTitle,
featuredImage: this.featuredImage,
creatorsName: this.creatorsName,
creatorProfile: this.creatorProfile,
creatorImage: this.creatorImage,
resourceOriginalLink: this.resourceOriginalLink,
resourceCategory: this.resourceCategory,
resourcePrice: this.resourcePrice,
downloadLink: this.downloadLink,
resourceDescription: this.resourceDescription,
date: this.date,
slug: this.slug

}).then(() => {
this.$router.push({ name: 'home'})
}).catch(err => {
console.log(err)
})
} else {
this.feedback = "You must enter a resource title"
}




}
}

}

目前,我还没有实现自动生成的“日期”,因为它不断破坏我的网站。但我确实使用了各种各样的东西,尤其是 -

firebase.firestore.FieldValue.serverTimestamp())

但是,也许我在尝试时将其合并错误,但似乎没有任何效果,而且我经常收到“Firebase 未定义”的错误。

以防万一,“从 firebase 导入数据库”看起来像这样 -

import firebase from 'firebase'
import 'firebase/firestore'
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};

const firebaseApp = firebase.initializeApp(config);
firebaseApp.firestore().settings({ timestampsInSnapshots: true })
export default firebaseApp.firestore()

我已经检查过这里 - https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp但仍然不知道如何将其实际输入到我的代码中并使其正常工作,我尝试的任何操作都会引发错误。

谢谢!

最佳答案

首先,导入 firebase 应该是这样的:

import firebase from 'firebase/app'
import 'firebase/firestore'

import firebase from 'firebase'
// import 'firebase/firestore' <- you dont need this
// this is an developing mode, so i would recommend the former one

第二,你应该了解import statement .

your-firebase-setting.js

import firebase from 'firebase/app'
import 'firebase/firestore'
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};

const firebaseApp = firebase.initializeApp(config);
firebaseApp.firestore().settings({ timestampsInSnapshots: true })
export default firebaseApp.firestore() // <------ HERE

HERE ,您要导出 Firestore 实例,不是 Firebase 实例,因此您可以像这样导入它:

SubmitResource.vue

<script>
import firestore from './your-firebase-setting'

export default {
name: 'SubmitResource'
// blah blah ...
}
</script>

并像这样使用它:

firestore.FieldValue.serverTimestamp()


以防万一,如果您同时需要 firebase 实例和 firestore 实例,您应该这样做:

import firebase from 'firebase/app'
import 'firebase/firestore'
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};

const firebaseApp = firebase.initializeApp(config);
firebaseApp.firestore().settings({ timestampsInSnapshots: true })
export { firebaseApp }
export const firestore = firebaseApp.firestore()

现在您可以像这样导入它们:

<script>
import { firebaseApp, firestore } from './your-firebase-setting'
</script>

那么下面的代码都可以工作:

firestore.FieldValue.serverTimestamp()

firebaseApp.firestore().FieldValue.serverTimestamp() // <- dont forget ()

关于Firebase Firestore 时间戳(如何实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com