JavaScript
x
28
28
1
import firebase from 'firebase/compat/app';
2
import 'firebase/compat/auth';
3
import 'firebase/compat/firestore';
4
import { doc, updateDoc,} from "firebase/firestore";
5
import { db } from "../api/firebase_config";
6
// const mydb = firebase.firestore()
7
import React from 'react'
8
const Mcheck = () => {
9
async function ResetCounter() {
10
// userRef.update({ FieldToIncrease: increment });
11
const dataTable = doc(db, 'mycollection', 'Number');
12
const increment = firebase.firestore.FieldValue.increment(1);
13
console.log(increment)
14
// await updateDoc(dataTable, data)
15
const res = await updateDoc(dataTable, {
16
sale: increment
17
})
18
}
19
function checkClick() {
20
ResetCounter()
21
}
22
return (
23
<div onClick={checkClick}><input type={"submit"} value='check' /></div>
24
)
25
}
26
27
export default Mcheck
28
i searched for many solutions but its all say somthing like this. so if any one can help me then it will be very good form me.
Advertisement
Answer
I think you’re mixing and matching SDK versions, or at least syntax.
Try using the increment
top-level function from the regular v9 SDK (not its compat
library).
JavaScript
1
11
11
1
// 👇
2
import { doc, updateDoc, increment } from "firebase/firestore";
3
4
// 👇 remove this
5
//const increment = firebase.firestore.FieldValue.increment(1);
6
//console.log(increment)
7
8
const res = await updateDoc(dataTable, {
9
sale: increment(1) // 👈 this increment is now the function we imported from firestore
10
})
11