Ionic and Firebase .update(): Nested Arrays are not supported - firebase

I'm having this issue while I'm trying to perform a crud update function. To put into context, this is an Ionic app with Firebase. This is an app in which the user will be able to create events and update them at a later stage if they want. However, I'm not being able to perform the update with the following error:
ERROR FirebaseError: Function DocumentReference.update() called with invalid data. Nested arrays are not supported (found in document Events/XWtRgH04iEG9IUIqMrgX)
Below are highlighted the function that will save an event after being updated and the service that contains the update function. Any help is greatly appreciated!
saveEvent(event) {
let id = event.id;
let evtSave = {
id: id,
createdAt: event['createdAt'],
createdBy: event['createdBy'],
updatedAt: Date.now(),
part: event['part'] || ['No participants'],
comments: event['comments'] || ['No comments'],
type: event['type'],
title: event['title'],
date: event['date'],
time: event['time'],
map: event['map'],
players: event['players'],
location: event['location'],
description: event['description'],
image: event['image']
};
console.log('saveEvent: ', evtSave);
this.eventServ.updateEvents(id, evtSave)
.then(res => {
this.searchEvents();
console.log('Event: ', res);
this.myAlert('Event successfully updated');
this.mode = 'listMode';
});
Below is the code contained in the service:
updateEvents(eventID, event){
return this.firestore.collection('Events').doc(eventID).update(({
id: event.id,
createdAt: event.date,
createdBy: event.createdBy,
updatedAt: Date.now(),
part: [event.part],
comments: [],
type: event.type,
title: event.title,
date: event.dateMilis,
time: event.time,
map: event.map,
players: event.players,
location: event.location,
description: event.description || 'No description...',
image: event.image || 'No image...',
})).catch((error)=>{
console.log('Error: ', error);
})
and finally a screenshot of how an event looks like in firebase:

At one place in your code you have part: event['part'] || ['No participants'], which will set part to an array of stringy, containing exactly one participant.
Later, when you save, you do: part: [event.part], which I assume can lead to the case where you will get part: [['No participants']].
This is a nested array and as firebase tells you in the error message, this is not supported in firestore.

Related

How do I delete user analytics data from Firebase using userDeletionRequests:upsert?

Problem Description
My Android app collects data via Google Analytics for Firebase. For privacy reasons, users must be able to wipe their data off the Firebase servers, should they choose to do so.
The app requests a deletion by forwarding its Firebase APP_INSTANCE_ID to my own server. This server has been prepared in advance with credentials, from my personal Google account (via oauth2), for managing the Firebase project. The server authenticates with www.googleapis.com, and, using the supplied APP_INSTANCE_ID, invokes the upsert.
As noted by the documentation, the generic Google Analytics API is appropriate for this task.
After some initial trouble (b/c I didn't have the correct auth scope, and the Analytics API wasn't properly enabled), googleapis.com now returns HTTP 200 for each upsert request. (As an aside, even if you supply a bogus APP_INSTANCE_ID, it returns 200.)
Here is a sample response from the upsert, which shows nothing amiss:
{ kind: 'analytics#userDeletionRequest',
id:
{ type: 'APP_INSTANCE_ID',
userId: (REDACTED 32-char hexidecimal string) },
firebaseProjectId: (REDACTED),
deletionRequestTime: '2018-08-28T12:46:30.874Z' }
I know the firebaseProjectId is correct, because if I alter it, I get an error. I have verified that the APP_INSTANCE_ID is correct, and stable up until the moment it is reset with resetAnalyticsData().
Test Procedure
To test the deletions, I populated Firebase with several custom events, using the procedure below (Nexus 5X emulator, no Google Play, no Google accounts configured, but that shouldn't make any difference):
Install the app
Fire off some custom events (FirebaseAnalytics.logEvent)
Observe those events appear on the Firebase console
(About a minute later:) Make the upsert call, observe HTTP 200, and note the "deletionRequestTime"
Immediately call FirebaseAnalytics.resetAnalyticsData (to clear any event data cached on the device)
Uninstall the app
Rinse & repeat 7 or 8 times
However, even 24 hours later, 100% of the Firebase events are still present in the events table. No discernable state change has taken place on the Firebase server as a result of the upserts.
Question
So, what am I doing wrong? how do I successfully delete user data from Google Analytics for Firebase?
EDIT
Here's the code I'm using to make a request (from node.js):
const request = require( 'request' );
...
_deletePersonalData( data )
{
return new Promise( (resolve, reject) => {
request.post({
url: 'https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert',
body: {
kind: 'analytics#userDeletionRequest',
id: {
type: 'APP_INSTANCE_ID',
userId: data.firebaseAppInstanceId
},
firebaseProjectId: (REDACTED)
},
headers: {
Authorization: 'Bearer ' + iap.getCurAccessToken()
},
json: true
}, (err, res, body) => {
console.log( 'user-deletion POST complete' );
console.log( 'Error ' + err );
console.log( 'Body ', body );
if( err )
{
reject( err );
return;
}
if( body.error )
{
reject( new Error( 'The Google service returned an error: ' + body.error.message + ' (' + body.error.code + ')' ) );
return;
}
resolve({ deletionRequestTime: body.deletionRequestTime });
});
});
}
Here's a sample request body:
{
kind: 'analytics#userDeletionRequest',
id: {
type: 'APP_INSTANCE_ID',
userId: (REDACTED 32-char hexidecimal string)
},
firebaseProjectId: (REDACTED)
}
And here's the console output for that same request (same userId and everything):
user-deletion POST complete
Error: null
Body: { kind: 'analytics#userDeletionRequest',
id:
{ type: 'APP_INSTANCE_ID',
userId: (REDACTED 32-char hexidecimal string) },
firebaseProjectId: (REDACTED),
deletionRequestTime: '2018-08-29T17:32:06.949Z' }
Firebase support just got back to me, and I quote:
Upsert method deletes any individual user data we have logged, but aggregate metrics are not recomputed. This means that you might not see any changes in the events tab in your Analytics console.
So, basically my mistake was expecting the events to disappear from the console.
This, of course, raises the question of how one determines that the API is actually working... but maybe the HTTP 200 is enough.

How to push or update data with emberfire?

Edit: See solution at the end
My guess was to put the model (in my case 'user') inside => type, but then it'll say "Assertion failed, you need to pass a model ..."
I do have a user.js in app/models
here's an excerpt from the router (after login function)
self.store.push({
data: {
id: data.currentUser.uid,
type: 'user',
attributes: {
displayName: data.currentUser.displayName,
email: data.currentUser.email,
photoURL: data.currentUser.photoURL,
firebaseUID: data.currentUser.uid,
rank: "scorer",
status: "active",
loginCount: 0,
provider: provider,
timestamp: new Date().getTime()
}
}
});
and here's my model (user.js in app/models)
import DS from 'ember-data';
export default DS.Model.extend({
displayName: DS.attr('string'),
email: DS.attr('string'),
photoURL: DS.attr('string'),
firebaseUID: DS.attr('string'),
rank: DS.attr('string'),
status: DS.attr('string'),
loginCount: DS.attr('string'),
provider: DS.attr('string'),
timestamp: DS.attr('number')
});
Please help :( thanks everyone in advance!
Edit => Solution that worked: If you do a createRecord and match the "id:" attribute, it will update the record with the same id (will work if you specified your own id). However, I'm not sure yet how to update a record if you let the system generate an ID for you. I assume that you would have to extract the ID first. But I haven't tested that idea yet. If someone would be so kind to test it, that'll be awesome.
Instead of pushing the raw data, create a model and run its save method.
var user = this.store.createRecord('user', {
displayName: data.currentUser.displayName,
// set more properties here
})
user.save()
This way, Emberfire and Ember Data can do their thing and ensure the data is formatted correctly. Also see
https://github.com/firebase/emberfire/blob/master/docs/quickstart.md#5-save-data
https://guides.emberjs.com/v3.0.0/models/creating-updating-and-deleting-records/
https://guides.emberjs.com/v3.0.0/models/pushing-records-into-the-store/

meteor autocomplete server-side

I'm writing a meteor app and I'm trying to add an autocomplete feature to a search box. The data is very large and is on the server, so I can't have it all on the client. It's basically a database of users. If I'm not wrong, the mizzao:autocomplete package should make that possible, but I can't seem to get it to work.
Here's what I have on the server:
Meteor.publish('autocompleteViewers', function(selector, options) {
Autocomplete.publishCursor(viewers.find(selector, options), this);
this.ready();
});
And here are the settings I use for the search box on the client:
getSettings: function() {
return {
position: 'bottom',
limit: 5,
rules: [{
subscription: 'autocompleteViewers',
field: '_id',
matchAll: false,
options: '',
template: Template.vLegend
}],
};
}
But I keep getting this error on the client:
Error: Collection name must be specified as string for server-side search at validateRule
I don't really understand the problem. When I look at the package code, it just seems like it's testing whether the subscription field is a string and not a variable, which it is. Any idea what the problem could be? Otherwise is there a minimum working example I could go from somewhere? I couldn't find one in the docs.
Error: Collection name must be specified as string for server-side search at validateRule
You get this error because you don't specify a Collection name in quotes.
getSettings: function() {
return {
position: 'bottom',
limit: 5,
rules: [{
subscription: 'autocompleteViewers',
field: '_id',
matchAll: false,
collection: 'viewers', // <- specify your collection, in your case it is a "viewers" collection.
options: '',
template: Template.vLegend
}],
};
}
For more information please read here.
Hope this helps!

Trouble Accessing Set Context in Collection2 to Display Invalid Keys on CLIENT

I am using Collection2 for form insert and validation. It works great.
My only issue using the context to access the keys for presenting errors back to the user on the client.
I have the following code:
Common.coffee
Schemas = {}
Schemas.Journal = new SimpleSchema
goal:
type: String
label: "Related Goal"
max: 200
description:
type: String
label: "Comment"
max: 200
likes:
type: Number
label: "Likes"
min: 0
createdBy:
type: String
max: 50
createdAt:
type: Date
label: "Created At"
Journal.attachSchema(Schemas.Journal)
journalContext = Schemas.Journal.namedContext("insertForm")
On Client:
Template.journalForm.events
'submit #newEntryForm': (event) ->
text = event.target.text.value
Meteor.call("newJournalEntry", Session.get("activeGoal"), text)
On Server as a Method:
'newJournalEntry': (goalId, text) ->
Journal.insert
goal: goalId
description: text
createdAt: new Date()
createdBy: Meteor.userId()
likes: 0
{validationContext: "insertForm"}, (error, result) ->
if error
console.log error.invalidKeys
else
console.log "#{result} added to Journal collection."
The validation works correctly on the server and when insert is denied I see the correct messages via terminal, but calling the validation context ON THE CLIENT always gives back an empty array. []
Either of the following work on the server, but if I try these on the client they are empty:
Schemas.Journal.namedContext("insertForm").invalidKeys()
or
error.invalidKeys
UPDATE:
I tried a few more tries at the syntax ON THE CLIENT. Same empty array result. Here are the attempts:
Schemas.Journal.namedContext().invalidKeys()
journalContext.invalidKeys()
Schemas.Journal.namedContext("insertForm").invalidKeys()

Update Mongo on the client using document ID, but limiting search with another field

When updating a document which contains an array of objects, I have this function:
Template.editCompetition.events = {
'click button.promote': function() {
Competitions.update({_id: Session.get('competition_id'), 'players.user_id': this.user_id}, {$set: {'players.$.supervisor': true}});
},
};
I am seeing the error:
Uncaught Error: Not permitted. Untrusted code may only update documents by ID.
My document looks like this:
{
_id: 'aaaabbbbb',
title: 'ccccdddddd',
players: [
{
username: 'wwwww',
user_id: 'xxxxx',
supervisor: false
},
{
username: 'yyyyy',
user_id: 'zzzzz',
supervisor: false
}]
}
Isn't my query updating the document by the ID, but also searching for that array position to use the .$ notation.
How can I achieve this without using a method, which seems a bit hacky...?
You are also trying to use the player.user_id as second parameter for update query
Other option you can have to use Meteor.method.
Move Collection update code to Meteor server side method and call it from client side
On Server side,
Meteor.methods({
`update_players`: function(competition_id){
user_id = this.userId
Competitions.update({_id: competition_id, 'players.user_id': user_id}, {$set: {'players.$.supervisor': true}});
}
})
In in above code, understand use of how we fetched current userId with this.userId
and now from your click handler call this meteror method
Meteor.call('update_players')
Hope this helps

Resources