Javascript 如何使用';添加';我弄错了?Vue js
我想在触发函数(单击)时添加一个新文档。它与Javascript 如何使用';添加';我弄错了?Vue js,javascript,firebase,vue.js,google-cloud-firestore,Javascript,Firebase,Vue.js,Google Cloud Firestore,我想在触发函数(单击)时添加一个新文档。它与.set()配合使用,但我希望在发布表单时创建每个新文档 这是我现在得到的错误代码: 我想我的连接可能有什么问题,但看起来不错 我的数据库连接文件 import firebase from 'firebase' import 'firebase/firestore' let config = { apiKey: "", authDomain: "", databaseURL: &qu
.set()
配合使用,但我希望在发布表单时创建每个新文档
这是我现在得到的错误代码:
我想我的连接可能有什么问题,但看起来不错
我的数据库连接文件
import firebase from 'firebase'
import 'firebase/firestore'
let config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
// Get a Firestore instance
export const db = firebase.initializeApp(config).firestore()
我试图在谷歌上搜索,但什么也找不到
AddProduct.vue-only脚本
<script>
/*eslint-disable-line*/import { db } from '../Database';
import firebase from 'firebase';
export default {
firestore: {
Products: db.collection('Products')
},
data() {
return {
sort: ['Fruit', 'Vegetable'],
fruit: ['Apple', 'Bananan', 'Grape', 'Kiwi', 'Lemon', 'Mango', 'Orange', 'Pear', 'Pineapple', 'Strawberry', 'Watermelon'],
vegetable: ['Cabbage', 'Radish', 'Carrot', 'Parsnip', 'Lettuce', 'Green bean', 'Aubergine', 'Tomato', 'Cucumber', 'Sweet pepper'],
quality: [1,2,3,4,5,6,7,8,9,10],
cours: ['€', '$', '£', '₺'],
random: Math.floor(Math.random() * 1000000),
addProducts: {
catogorie: '',
product: '',
quality: '',
availability: '',
cours: '',
price: '',
firstName: '',
lastName: '',
companyName: '',
email: '',
location: '',
country: '',
}
}
},
methods: {
async add() {
var currentUser = firebase.auth().currentUser.uid
const res = await db.collection('Products').doc(currentUser + this.random).add({
addProducts: this.addProducts
});
console.log(res)
}
},
created() {
console.log(this.random)
}
}
</script>
/*eslint从“../Database”禁用行*/import{db};
从“firebase”导入firebase;
导出默认值{
firestore:{
产品:数据库集合(“产品”)
},
数据(){
返回{
分类:[“水果”、“蔬菜”],
水果:[‘苹果’、‘香蕉’、‘葡萄’、‘猕猴桃’、‘柠檬’、‘芒果’、‘橘子’、‘梨’、‘菠萝’、‘草莓’、‘西瓜’],
蔬菜:[“卷心菜”、“萝卜”、“胡萝卜”、“防风草”、“莴苣”、“绿豆”、“茄子”、“番茄”、“黄瓜”、“甜椒”],
质量:[1,2,3,4,5,6,7,8,9,10],
课程:['欧元','美元','英镑','₺'],
随机:Math.floor(Math.random()*1000000),
添加产品:{
卡托戈里:“,
产品:“”,
品质:'',
可用性:'',
当然:'',
价格:'',
名字:'',
姓氏:“”,
公司名称:'',
电子邮件:“”,
位置:“”,
国家:“,
}
}
},
方法:{
异步添加(){
var currentUser=firebase.auth().currentUser.uid
const res=await db.collection('Products').doc(currentUser+this.random)。添加({
addProducts:this.addProducts
});
console.log(res)
}
},
创建(){
console.log(this.random)
}
}
我绘图不好,但红色块必须是相同格式的新文档,但数据不同。
您的代码正在尝试对DocumentReference类型对象调用
add()
:
await db.collection('Products').doc(currentUser + this.random).add({
addProducts: this.addProducts
});
这是无效的,因为没有名为“add”的方法
如果要使用DocumentReference编写文档,则应调用
add()
是上的一个方法,它使用新的随机生成的ID添加文档。由于您提供自己的ID,因此这没有帮助。但是,如果您愿意接受该随机ID,您可以这样编写代码:
await db.collection('Products').add({
addProducts: this.addProducts
});
您的代码试图对DocumentReference类型对象调用
add()
:
await db.collection('Products').doc(currentUser + this.random).add({
addProducts: this.addProducts
});
这是无效的,因为没有名为“add”的方法
如果要使用DocumentReference编写文档,则应调用
add()
是上的一个方法,它使用新的随机生成的ID添加文档。由于您提供自己的ID,因此这没有帮助。但是,如果您愿意接受该随机ID,您可以这样编写代码:
await db.collection('Products').add({
addProducts: this.addProducts
});