Closed suigeneris closed 5 years ago
Hi @suigeneris can you paste the actual code for your function? It's very hard to understand what is happening without more information. Also, if you could paste the logs that would be helpful as well.
Hi @thechenky here is the function and the corresponding logs, as we can see from the logs, the function completes with a status code of 200. But the data is not reflected in firebase.
code:
var functions = require('firebase-functions');
var rp = require('request-promise');
var r = require('request');
var teamInfoRequest = {
uri: 'https://api.fantasydata.net/v3/soccer/stats/json/SeasonTeams/64',
headers: {
'Ocp-Apim-Subscription-Key': '*******************************'
},
json: true
};
// Function to construct Object for fetching players for the provided TeamID
function configurePlayersByTeamRequest(teamIdentification) {
return request = {
teamID: null,
teamName: null,
uri: 'https://api.fantasydata.net/v3/soccer/scores/json/PlayersByTeam/' + teamIdentification,
headers: {
'Ocp-Apim-Subscription-Key': '*******************************'
},
json: true
}
}
function fetchTeamIdsAndTeamName(admin, response) {
const promise = new Promise(function (resolve, reject) {
r.get(teamInfoRequest, function (err, fullResponse, body) {
if (err) {
reject(err)
} else {
resolve(body)
}
})
})
.then(function (body) {
return body.map(function(responseData) {
return {
"teamID" : responseData.TeamId,
"teamName" : responseData.TeamName
}
})
})
.then(function (teamInfoArray) {
var arry = teamInfoArray.map(function(teamInfo) {
var requests = configurePlayersByTeamRequest(teamInfo.teamID)
requests.teamID = teamInfo.teamID
requests.teamName = teamInfo.teamName
return requests
})
return arry
})
.then(function (mappedRequests) {
return mapRequestsIntoPromises(mappedRequests)
})
.then(function (mappedPromisesData) {
var playerData = mappedPromisesData.map(function (bodyData) {
return nestedBodyData = bodyData.map(function (data) {
return {
'player_id' : data.PlayerId,
'player_number' : data.Jersey,
'player_name' : data.CommonName,
'player_position' : data.Position,
'team_name' : data.PlayerTeam
}
})
}).reduce(function (flat, toFlatten) {
return flat.concat(toFlatten);
}).filter(function (data) {
return data.player_number != null
})
return playerData
}).then(function (playerdata) {
var groupPlayersIntoclubsPromises = playerdata.map(function (data) {
return admin.firestore().doc(`leagues/premierLeague/clubs/${data.team_name}/squad/${data.player_name}`).set(data)
})
var allPlayersPromise = playerdata.map(function (data) {
return admin.firestore().doc(`leagues/premierLeague/players/${data.player_name}`).set(data)
})
console.log('success')
response.send(playerdata)
})
.catch(function (error) {
console.log(error)
response.status(500).send(error)
})
return promise
}
function mapRequestsIntoPromises(mappedRequest) {
var allPromises = mappedRequest.map(function (req) {
const promise = new Promise(function (resolve, reject) {
r.get(req, function (err, response, body) {
if (err) {
reject(err)
} else {
var bodyWithTeamData = body.map(function (eachBody) {
eachBody['PlayerTeam'] = req.teamName
return eachBody
})
resolve(bodyWithTeamData)
}
})
})
return promise
})
return Promise.all(allPromises)
}
module.exports = {
loadData: function (admin, response) {
return fetchTeamIdsAndTeamName(admin, response)
}
};
logs:
6:44:09.266 PM | outlined_flag | populatePlayerAndTeamData | Function execution took 2643 ms, finished with status code: 200
6:44:06.624 PM | outlined_flag | populatePlayerAndTeamData | Function execution started
but when i run the same function locally using localhost, i get the same logs and i can see the data reflected in firestore.
@suigeneris Thank you for providing the code. When you say that it "works" locally - are you saying that the write to Firestore completes when serving the function locally, while your production function does not write to Firestore? You mentioned earlier that the updates to Firestore "do not show up immediately" - if this is the case, it's likely due to the cold start after you deploy your function, which is normal, and you wouldn't see this cold start locally. So this could just be the infrastructure working normally. Please confirm.
@thechenky yes, by locally i mean serving the functions using firebase serve --only functions
.
I think youre right, i imagined it being due to a cold start, does anyone know how long the cold start duration is? because i deployed my functions to production days ago and running them still doesnt update the data in Firestore
@suigeneris thanks for the update! Cold starts should be on the order of seconds, with an update after a deploy taking ~30s to propagate. If your functions are not writing to Firestore after days (and this is with them being triggered), then the problem is likely with the data you're trying to write to Firestore. I would look at the differences between the data you're writing when serving locally and the production data. There might be some exceptions that you're not catching that are getting triggered in production. I would also add more console.log
statements to see if you can pinpoint where the issue is occurring.
@thechenky its exactly the same data in both cases, same code, same everything. Another potential issue could be the size of the data, perhaps theres a limit on how much data a function can write to the firestore at any given time. As you have suggested, i will put more logs to try and pin point the issue.
@suigeneris any updates?
Closing for inactivity.
Hello
**Version info firebase-functions: 2.1.0
firebase-tools: 6.1.2
firebase-admin: 6.4.0**
iv written a number of cloud functions that seem to work perfectly fine when i deploy locally using the following command:
firebase serve --only functions
However when i deploy the same functions remotely using:
firebase deploy --only functions
The function does not update the data in the firestore. Iv checked the logs, and all the responses, everything checks out fine, the data just doesn't show up in firestore. Im am baffled by this, since its clear to me that there is nothing wrong with the code.I believe the problem lies between deploying functions locally vs remotely. Perhaps there is some limit on how much data can be written at any given time.
Has anybody experienced this?, any information will be useful.