尝试记录提交时间时出现问题(以便以后可以过滤),然后提交给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,但仍然不知道如何将其实际输入到我的代码中并使它起作用,我尝试进行的任何操作都会引发错误。
谢谢!
答案 0 :(得分:1)
首先,导入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 ()
答案 1 :(得分:0)
初始化firebase文件
glm(my_var1 ~ my_var2, family = binomial(my_link))
然后致电
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.serverTimestamp = firebase.firestore.FieldValue.serverTimestamp()
export default firebaseApp