'How to make query api for email that I can find data email wise for different users using Node.js, Express.js and MongoDB
Client-side code:
const [myInventory, setMyInventory] = useState([]);
const [user] = useAuthState(auth);
useEffect(() => {
const getMyInventory = async () => {
const email = user?.email;
console.log(email);
const url = `http://localhost:5000/myInventory/?email=${email}`;
try {
await fetch(url)
.then(res => res.json())
.then(data => setMyInventory(data))
} catch (error) {
console.log(error?.message)
}
}
getMyInventory();
}, [user])
Server-side code:
app.get('/myInventory/', async (req, res) => {
const email = req.query.email;
const query = { email: email };
const cursor = inventoryCollection.find(query);
const myInventory = await cursor.toArray();
console.log(myInventory);
res.send(myInventory);
});
Solution 1:[1]
I solved this problem using axios method
useEffect(() => {
const getInventory = async () => {
const email = user?.email;
const url = `http://localhost:5000/myInventory?email=${email}`;
const { data } = await axios.get(url, {
headers: {
authorization: `Bearer ${localStorage.getItem('accessToken')}`
}
});
setMyInventory(data);
}
getInventory();
}, [user, myInventory])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tasos K. |
