I am encoding a function that launches API calls and requests JSON from a huge database sequentially using offsets. The JSON response is parsed, and then subsequent data internally is uploaded to our Cloud Firestore server.
Nodejs (Node 6.11.3) and the latest Firebase Admin SDK
The information is parsed as expected and prints fine on the console. However, when data is being tried to load into our Firestore database, the console is sent as spam with an error message:
Authentication Error: Error: Socket Hang
(node: 846) UnhandledPromiseRejectionWarning: rejection of an unhandled promise (rejection id: -Number-): Error: failure to receive metadata from the plugin Error: socket hung up
and sometimes:
Authentication Error: Error: Read ECONNRESET
The forEach function collects elements from loaded JSON and processes the data before loading it into the Firestore database. Each JSON has up to 1000 data elements (1000 documents) for passing through the forEach function. I understand that this can be a problem if the function is repeated until the download installation is complete?
I am new to encoding and understand that the control flow of this function is not the best. However, I cannot find the error information that the console prints. I can find a lot of information about socket hangs, but there is not one in the Auth error section.
JSON , firebase-adminsdk. / , ( ).
:
Firebase
const admin = require('firebase-admin');
var serviceAccount = require("JSON");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "URL"
});
var db = admin.firestore();
var offset = 0;
var failed = false;
HTTP
var runFunction = function runFunction() {
var https = require('https');
var options = {
host: 'website.com',
path: (path including an offset and 1000 row specifier),
method: 'GET',
json: true,
headers: {
'content-type': 'application/json',
'Authorization': 'Basic ' + new Buffer('username' + ':' + 'password').toString('base64')
}
};
HTTP- , API
if (failed === false) {
var req = https.request(options, function (res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', () => {
console.log('Successfully processed HTTPS response');
body = JSON.parse(body);
if (body.hasOwnProperty('errors')) {
console.log('Body ->' + body)
console.log('API Call failed due to server error')
console.log('Function failed at ' + offset)
req.end();
return
} else {
if (body.hasOwnProperty('result')) {
let result = body.result;
if (Object.keys(result).length === 0) {
console.log('Function has completed');
failed = true;
return;
} else {
result.forEach(function (item) {
var docRef = db.collection('collection').doc(name);
console.log(name);
var upload = docRef.set({
thing: data,
thing2: data,
})
});
console.log('Finished offset ' + offset)
offset = offset + 1000;
failed = false;
}
if (failed === false) {
console.log('Function will repeat with new offset');
console.log('offset = ' + offset);
req.end();
runFunction();
} else {
console.log('Function will terminate');
}
}
}
});
});
req.on('error', (err) => {
console.log('Error -> ' + err)
console.log('Function failed at ' + offset)
console.log('Repeat from the given offset value or diagnose further')
req.end();
});
req.end();
} else {
req.end();
}
};
runFunction();
!
UPDATE
JSON, , , - 1000 100. , .
, forEach .
# 2
, async.eachSeries . , ( 9 158 000 ). , :
async.eachSeries(result, function (item, callback) {
var docRef = db.collection('collection').doc(name);
console.log(name);
var upload = docRef.set({
thing: data,
thing2: data,
}, { merge: true }).then(ref => {
counter = counter + 1
if (counter == result.length) {
console.log('Finished offset ' + offset)
offset = offset + 1000;
console.log('Function will repeat with new offset')
console.log('offset = ' + offset);
failed = false;
counter = 0
req.end();
runFunction();
}
callback()
});
});
, :
(node: 16168) UnhandledPromiseRejectionWarning: ( : -Number-): : .
, ... . - - , ?