Reputation: 3396
I use the Firebase SDK with Node.js and try to delete a bulk of accounts programmatically (identified by their email address).
What does work is to hardcode those accounts in my firebase-delete-users.js file like this:
const uids = [
{email: '[email protected]'},
{email: '[email protected]'},
];
const markedForDelete = [];
// Retrieve a batch of user accounts.
admin.auth().getUsers(uids)
.then((result) => {
result.users.forEach((user) => {
markedForDelete.push(user.uid);
});
result.notFound.forEach((uid) => {
console.log(`No user found for identifier: ${JSON.stringify(uid)}`)
});
})
.then(() => {
// Delete all marked user accounts in a single API call.
return admin.auth().deleteUsers(markedForDelete);
});
But it does no longer work if I try to get those elements out of a .txt file, where each line is one array-element.
var uids = fs.readFileSync("accounts-to-delete.txt", 'utf8').toString().split("\n");
accounts-to-delete.txt :
{email: '[email protected]'}
{email: '[email protected]'}
The console log also doesn't look exactly the same if I compare uids and uids2 but I don't understand the reason and how to solve it:
var uids = fs.readFileSync("accounts-to-delete.txt", 'utf8').toString().split("\n");
const uids2 = [
{email: '[email protected]'},
{email: '[email protected]'},
];
for(i in uids) {
console.log(uids[i]);
}
for(i in uids2) {
console.log(uids2[i]);
}
If it helps to find a solution:
It doesn’t have to be a .txt file if .json or some other filetype fits better for this use case.
Upvotes: 1
Views: 45
Reputation: 3043
fs.readFileSync("accounts-to-delete.txt", 'utf8').toString().split("\n");
returns an array of strings instead an array or objectsm which is what you want.
So you can use JSON.parse()
to parse each split line.
Upvotes: 1
Reputation: 317372
Reading text from a file and iterating elements of an array of objects are completely different tasks. They have little on common, other than iteration.
Your bit of code that iterates an array of objects is simply taking advantage of the fact that the data in uids2
is already structured as Javascript arrays and object. It doesn't work the same for reading lines of raw text out of a file.
Your bit of code that reads the file is not able to assume the JSON structure of the data in its raw form. uids
is just an array of strings. You will have to parse the JSON and turn each line into actual JavaScript objects using JSON.parse(). Something like this might work:
var uids = fs
.readFileSync("accounts-to-delete.txt", 'utf8')
.toString()
.split("\n")
.map(line => JSON.parse(line));
With this uids
not just an array of strings, as it was before. It's now an array of JavaScript objects that are easier to work with.
Upvotes: 3