Reputation: 312
Just a really simple question for cloud functions. I am just wondering why is the data in my firestore (in the simulator) appears to be:
useruid
Item Name: "Yummy Food"
Item Price: "25"
Item Quantity: "50"
and not what I wanted which is:
qwjeqjwioej123123123
Item Name: "Yummy Food"
Item Price: "25"
Item Quantity: "50"
The below is my code:
exports.addCurrentOrder = functions.https.onRequest(async (req, res) => {
const useruid = req.query.uid;
const itemName = req.query.itemName;
const itemPrice = req.query.itemPrice;
const itemQuantity = req.query.itemQuantity;
console.log('This is in useruid: ', useruid);
const data = { useruid : {
'Item Name': itemName,
'Item Price': itemPrice,
'Item Quantity': itemQuantity,
}};
const writeResult = await admin.firestore().collection('Current Orders}').add(data);
res.json({result: data});
});
Sorry I dont have much JS expereince
Upvotes: 0
Views: 29
Reputation: 317372
Your code is indeed literally putting that string into the document as the name of the field. That's how JavaScript objects work.
If you want to substitute the value of a variable as the name of an object property, you can use square bracket syntax to provide express that evaluates to the string to use:
const data = { [useruid] : {
'Item Name': itemName,
'Item Price': itemPrice,
'Item Quantity': itemQuantity,
}};
Upvotes: 1