Я использую облачную функцию для загрузки своей коллекции БД в виде файла JSON.
Код
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)
})
)
})
Большую часть времени меня волнует только value
, но иногда value
вложено внутрь .. и когда оно будет вложено, мне понадобится value
и name
токовый выход
[{
"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
}
},
}]
желаемый выход:
[{
"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",
}
},
}]
Я пытался
answer.answers.map(questionId => {
return console.log('value', questionId.value)
})
, но answers.answers.map
не является функцией