Firebase Cloud Function I want to change all user data - firebase

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 20 * * *')
.timeZone('Asia/Seoul') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) =>
{
var allUsers = [];
return admin.auth().listUsers()
.then(function (listUsersResult)
{
listUsersResult.users.forEach(function (userRecord)
{
// For each user
var userData = userRecord.toJSON();
allUsers.push(userData.uid);
console.log(allUsers);
});
//res.status(200).send(JSON.stringify(allUsers));
}).then(function ()
{
allUsers.forEach(function (elem)
{
db.ref(`Data/${elem}/Enter`).update({ test: 0, test2: 0 });
});
})
.catch(function (error)
{
console.log("Error listing users:", error);
//res.status(500).send(error);
});
});
By getting the UIDs of all users at a specific time
I want to change the data with UID as parent But An error has occurred
Each then() should return a value or throw promise/always-
Could you please let me know what's wrong with my code?

Maybe try adding a return statement to each then block like so:
return admin.auth().listUsers()
.then(function (listUsersResult)
{
listUsersResult.users.forEach(function (userRecord)
{
// For each user
var userData = userRecord.toJSON();
allUsers.push(userData.uid);
console.log(allUsers);
});
return true; //<--------------- add this return statement
}).then(function ()
{
allUsers.forEach(function (elem)
{
db.ref(`Data/${elem}/Enter`).update({ test: 0, test2: 0 });
});
return true; //<--------------- add this return statement
})
.catch(function (error)
{
console.log("Error listing users:", error);
//res.status(500).send(error);
});

Related

How to get multiple references in a firestore snapshot

I have a firestore collection containing post documents, each document contains a reference to an author (user) and a case document.
How do I get the user and the case in the same onSnapshot?
Here's what I'd like to do with await, but that doesn't seem to be an option with react-native-firebase.
export const firebasePostLooper = (snapshot) => {
let data = [];
snapshot.forEach(async (doc) => {
let newItem = {id: doc.id, ...doc.data()};
if (newItem.author) {
let authorData = await getDoc(newItem.author); // doesn't work with rnfirebase
if (authorData.exists()) {
newItem.userData = {userID: authorData.id, ...authorData.data()};
}
}
if (newItem.case) {
let caseData = await getDoc(newItem.case);
if (caseData.exists()) {
newItem.userData = {userID: caseData.id, ...caseData.data()};
}
}
data.push(newItem);
});
return data;
};
This doesn't work because getDoc() doesn't exist.
So I'm left with using .then()
export const firebasePostLooper = (snapshot) => {
let data = [];
snapshot.forEach((doc) => {
let newItem = {id: doc.id, ...doc.data()};
if (newItem.author) {
newItem.author
.get()
.then((res) => {
newItem.authorData = res.data();
if (newItem.case) {
newItem.case
.get()
.then((caseRes) => {
newItem.caseData = caseRes.data();
data.push(newItem);
})
.catch((err) => console.error(err));
}
})
.catch((err) => console.error(err));
} else {
data.push(newItem);
}
});
return data;
};
This second method doesn't seem to be working, data is empty at the return statement but data.push(newItem) contains the correct document with the 2 referenced documents.
You're returning data before it gets filled inside the promise. You should handle the returning of the data inside a .then() in order to return it after the promise has resolved and not before.
Take a look at this example where if we handle the emptyData object outside the promise chain, we just return the initial value before it has been filled.
let promise = new Promise((resolve, reject)=>{
setTimeout(resolve, 1000, 'foo');
})
let emptyData= [];
let notEmptyData = [];
promise
.then(res=>{
emptyData.push(res);
notEmptyData.push(res);
console.log("Full data: " + notEmptyData) // "Full data: foo"
});
console.log("Empty data: " + emptyData); // "Empty data: "

How can I update a document found in a query?

I want to update document found by a query. Actually I'm trying the following approach:
export const add_user = functions.https.onRequest((request, response) => {
corsHandler(request, response, () => {
const collectionReference = db.collection("users");
const query = collectionReference.where("name", "==", "John");
fbid_query.get()
.then(function(querySnapshot) {
if (querySnapshot.empty) {
console.log("John notfound");
} else {
console.log("John found!");
querySnapshot.forEach(function (documentSnapshot) {
add_update(query, "George");
});
}
response.json([]);
})
.catch(function(error) {
response.json([]);
});
});
});
.
const add_update = function (query, new_name) {
query.get(function(querySnapshot) {
querySnapshot.forEach(function(document) {
console.log("entered in forEach");
document.ref.update({
name: new_name
});
});
}).then(function () {
console.log("success");
})
.catch(function(error) {
console.error("error: ", error);
});
}
The only console.log that appears is success, although nothing is updated and John indeed exists in Cloud Firestore.
What am I doing wrong?
PS: I don't know if it matters but before I actually try to update, I call query.get() just to check if something exists (and after I call that approach to update).

Update data if exist in firebase

I'm trying to update a data if it exists
var ref = firebase.database().ref().child('users');
var refUserId = firebase.database().ref().child('users').orderByChild('id').equalTo(Auth.$getAuth().uid);
refUserId.once('value', function (snapshot) {
console.log(snapshot);
if (snapshot.exists()) {
snapshot.ref().update(vm.user_infos);
} else {
ref.push({
player: vm.user_infos.player,
id: vm.user_infos.id
}, function(error) {
console.log(error);
})
}
});
Push is working fine, but the update did not.
snapshot.ref is not a function
In the snapshot () log console:
I tried this way too:
if (snapshot.exists()) {
refUserId.update({
player: vm.user_infos.player,
id: vm.user_infos.id
}, function(error) {
console.log(error);
})
Result:
refUserId.update is not a function
User structure
The first problem is that the snapshot's ref property is an object - not a function.
The second is that the snapshot refers to the users path, so you should check for a user that matches your query like this:
var ref = firebase.database().ref().child('users');
var refUserId = ref.orderByChild('id').equalTo(Auth.$getAuth().uid);
refUserId.once('value', function (snapshot) {
if (snapshot.hasChildren()) {
snapshot.forEach(function (child) {
child.ref.update(vm.user_infos);
});
} else {
snapshot.ref.push({
player: vm.user_infos.player,
id: vm.user_infos.id
});
}
});
And if you want to know when the update or push has completed, you could use promises:
refUserId
.once('value')
.then(function (snapshot) {
if (snapshot.hasChildren()) {
return snapshot.forEach(function (child) {
child.ref.update(vm.user_infos);
});
} else {
return snapshot.ref.push({
player: vm.user_infos.player,
id: vm.user_infos.id
});
}
})
.then(function () {
console.log('update/push done');
})
.catch(function (error) {
console.log(error);
});

redux observable: Why don`t can get all actions in test

I'm trying to test a 'redux observable epic' but the test fail because not all actions are in store.getActions() the strange is the store.dispatch function runs.
Epic and actions
export const VERIFY_SESION = 'auth/VERIFY_SESION';
export const SET_POLICIES_ACCEPTED = 'auth/SET_POLICIES_ACCEPTED';
export const AUTHENTICATE = 'auth/AUTHENTICATE';
export function setPoliciesAccepted(wereAccepted: boolean) {
return {
wereAccepted,
type: SET_POLICIES_ACCEPTED,
};
}
export function verifySesion() {
return {
type: VERIFY_SESION,
};
}
export function authenticate(token) {
return {
token,
type: AUTHENTICATE,
};
}
export function verifySesionEpic(action$, store) {
return action$
.ofType(VERIFY_SESION)
.switchMap(async () => {
try {
store.dispatch(setBlockLoading(true));
const token = await AsyncStorage.getItem('token');
if (token !== null) {
store.dispatch(setBlockLoading(false));
return authenticate(token);
}
const policiesWereAccepted = await AsyncStorage.getItem('policiesWereAccepted');
store.dispatch(setBlockLoading(false));
return setPoliciesAccepted(policiesWereAccepted);
} catch (error) {
return setMessage(error.message);
}
});
}
test
describe('actions/auth', () => {
let store;
const asyncStorageGetStub = stub(AsyncStorage, 'getItem');
beforeEach(() => {
store = mockStore();
});
afterEach(() => {
asyncStorageGetStub.restore();
});
it('Should call authenticate if token', () => {
const token = 'mitoken';
asyncStorageGetStub.withArgs('token').returns(Promise.resolve(token));
store.dispatch(verifySesion());
expect(store.getActions()).toContain({ type: AUTHENTICATE, token });
});
});
Test result
1) "actions/auth Should call epic for verifySesion:
Error: Expected [ { type: 'auth/VERIFY_SESION' } ] to include { token: 'mitoken', type: 'auth/AUTHENTICATE' }"
Note
im sure that the conditional token !== null pass
I was to add a timeout before getAction because the 'AUTHENTICATE' actions is added after.
it('Should call authenticate if token', (done) => {
const token = 'mitoken';
asyncStorageGetStub.withArgs('token').returns(Promise.resolve(token));
store.dispatch(verifySesion());
setTimeout(() => {
expect(store.getActions()).toContain({ type: AUTHENTICATE, token });
done();
}, 1000);
});

Cordova SQLite: database already open: data.db

I am trying to implement a chat app in Ionic 2 and I use SQLite.
I am however, getting the following error when I execute a executeSql on the database.
TypeError {stack: (...), message: "Cannot read property 'executeSql'
of undefined"}
But when I open the databse each transaction, I don't get the above error, but get the following warning:
database already open: data.db
I have been reading the Ionic and Cordova documentation, but cannot seem to understand what I am doing incorrect. If anyone can advise, I would appreciate it.
chatsStorageService.ts
public openDatabase(): Promise<Array<Message>> {
let promise: Promise<Array<Message>> = new Promise<Array<Message>>(resolve => {
console.log('openDatabase: ', this.database);
if (this.database && this.database != null) {
return Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
resolve(this.messages);
});
} else {
this.database = new SQLite();
this.database.openDatabase({
name: "data.db",
location: "default"
}).then(() => {
return Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
resolve(this.messages);
});
}, (error) => {
console.log("OPEN ERROR: ", error);
});
}
});
return promise;
}
public refreshChats(db: any): Promise<Array<Chat>> {
let promise: Promise<Array<Chat>> = new Promise<Array<Chat>>(resolve => {
return db.executeSql("SELECT * FROM chats", [])
.then((chatData) => {
let promises: Array<any> = [];
this.chats = [];
if (chatData.rows.length > 0) {
for (var i = 0; i < chatData.rows.length; i++) {
promises.push(this.populateChat(db, chatData.rows.item(i)));
}
}
return Promise.all(promises).then(() => {
resolve(this.chats);
});
})
.catch(error => {
console.log("ERROR REFRESHING CHATS: " + JSON.stringify(error));
console.log(error);
});
});
return promise;
}

Resources