For my database collection, I am utilizing a cloud function to download it as a JSON file.
Here is the code snippet:
exports.csvJsonReport = functions.https.onRequest((request, response) => {
const db = admin.firestore()
const userAnswers = db.collection('/surveys/CNA/submissions')
return (
userAnswers
.get()
.then(querySnapshot => {
let answers = []
querySnapshot.forEach(doc => {
const answer = doc.data()
answers.push({ ...answer.answers, ...answer.anonUser })
})
response.setHeader('Content-disposition', 'attachment; filename=cna.json')
response.set('Content-Type', 'application/json')
response.status(200).send(answers)
})
.catch(error => {
console.log(error)
})
)
})
In most cases, I am interested in only the value
field. However, sometimes the value
is nested within another field, in which case I need both the value
and name
.
Current output:
[{
"2": {
"loopIndex": null,
"questionType": "multiChoice",
"value": "Farm owner",
"id": 1
},
"7": {
"1": {
"name": "Enviroment management",
"questionType": "responsiveMultiCheckBox",
"valueId": 4,
"value": "My top priority right now",
"id": 1
},
"2": {
"questionType": "responsiveMultiCheckBox",
"valueId": 3,
"value": "One of my top priorities",
"id": 2,
"name": "Financial management"
},
"3": {
"value": "My top priority right now",
"id": 3,
"name": "People management",
"questionType": "responsiveMultiCheckBox",
"valueId": 4
},
"4": {
"name": "Reproduction management",
"questionType": "responsiveMultiCheckBox",
"valueId": 4,
"value": "My top priority right now",
"id": 4
},
"5": {
"name": "Feed management",
"questionType": "responsiveMultiCheckBox",
"valueId": 4,
"value": "My top priority right now",
"id": 5
}
},
}]
Desired output:
[{
"2": {
"value": "Farm owner",
},
"7": {
"1": {
"name": "Enviroment management",
"value": "My top priority right now",
},
"2": {
"value": "One of my top priorities",
"name": "Financial management"
},
"3": {
"value": "My top priority right now",
"name": "People management",
},
"4": {
"name": "Reproduction management",
"value": "My top priority right now",
},
"5": {
"name": "Feed management",
"value": "My top priority right now",
}
},
}]
I attempted to use:
answer.answers.map(questionId => {
return console.log('value', questionId.value)
})
However, the error message "answers.answers.map is not a function" was encountered.