App Maker: Sending Email, client script to server script function not working. Failed due to illegal value in property: a - google-app-maker

I am new to AppMaker but I have developer experience.
The application is a Project Tracker Application
What I expect to happen: When creating a project the user uses a User Picker to select the users associated with that project. When the project is created I want to email the users associated with that project.
The issue: On clicking the Add button addProject(addButton) client script function is called.
Inside this function sendEmailToAssignees(project, assignees) is called which should reach out to the Server script and run the notifyAboutProjectCreated(project, assignees) but that is not happening.
Things to know: With logging I never reach 'Trying to send email' so I seem to never reach my server script. Also, On client script when I comment out sendEmailToAssignees function everything runs smooth. I have looked at this documentation as a resource so I feel my implementation is okay. https://developers.google.com/appmaker/scripting/client#client_script_examples
The final error message I get is:
Failed due to illegal value in property: a at addProject
(AddProject:110:24) at
AddProject.Container.PanelAddProject.Form1.Spring.ButtonAdd.onClick:1:1
Am I missing something here? Any help would be greatly appreciated. Thank you!
Client Script
function sendEmailToAssignees(project, assignees) {
google.script.run
.withSuccessHandler(function() {
console.log('Sending Email Success');
}).withFailureHandler(function(err) {
console.log('Error Sending Email: ' + JSON.stringify(err));
})
.notifyAboutProjectCreated(project, assignees);
}
function addProject(addButton) {
if (!addButton.root.validate()) {
return;
}
addButton.datasource.createItem(function(record) {
var page = app.pages.AddProject;
var pageWidgets = page.descendants;
var trainees = pageWidgets.AssigneesGrid.datasource.items;
var traineesEmails = trainees.map(function(trainee) {
return trainee.PrimaryEmail;
});
record.Assignee = traineesEmails.toString();
var assignees = traineesEmails.toString();
var project = record;
updateAllProjects(record);
console.log('update all projects done');
sendEmailToAssignees(project, assignees);
console.log('Send Email done');
if (app.currentPage !== app.pages.ViewProject) {
return;
}
gotoViewProjectPageByKey(record._key, true);
});
gotoViewProjectPageByParams();
}
Server Script
function notifyAboutProjectCreated(project, assignees) {
console.log('Trying to send email');
if (!project) {
return;
}
var settings = getAppSettingsRecord_()[0];
if (!settings.EnableEmailNotifications) {
return;
}
var data = {
appUrl: settings.AppUrl,
assignee: project.Assignee,
owner: project.Owner,
startDate: project.StartDate,
endDate: project.EndDate,
jobType: project.Type,
jobId: project.Id
};
// Email Subject
var subjectTemplate = HtmlService.createTemplate(settings.NotificationEmailSubjectJob);
subjectTemplate.data = data;
var subject = subjectTemplate.evaluate().getContent();
// Email Body
var emailTemplate =
HtmlService.createTemplate(settings.NotificationEmailBodyJob);
emailTemplate.data = data;
var htmlBody = emailTemplate.evaluate().getContent();
console.log('About to send email to:', assignees);
sendEmail_(null, assignees, subject, htmlBody);
}

The reason you are getting this error is because you are trying to pass the client "project record" to the server. If you need to access the project, then pass the record key to the server and then access the record on the server using the key.
CLIENT:
function sendEmailToAssignees(project, assignees) {
var projectKey = project._key;
google.script.run
.withSuccessHandler(function() {
console.log('Sending Email Success');
}).withFailureHandler(function(err) {
console.log('Error Sending Email: ' + JSON.stringify(err));
})
.notifyAboutProjectCreated(projectKey , assignees);
}
SERVER:
function notifyAboutProjectCreated(projectKey, assignees) {
console.log('Trying to send email');
var project = app.models.<PROJECTSMODEL>.getRecord(projectKey);
if (!project) {
return;
}
//Rest of the logic
}
The project record object in the client is not the same as the project record object in the server; hence the ilegal property value error.

Related

Querying another easy table for push notification message

I have two easy tables configured in my Azure App backend:
Services with Id and ServiceName properties
ServiceDetails with Id, ServiceID and ServiceDetailDate properties
Whenever a new ServiceDetails entry is inserted, I want to send a message to my users via Push Notifications containing information about ServiceDetailDate and ServiceName.
So my question is how can I query another table to obtain information from it? In this case the ServiceID (from ServiceDetails table) is known, so I want to get the ServiceName from Services. Which code should I add to extract the ServiceName from the serviceInfo object (in the payload message) to my script to fulfill this request?
Actually I'm not sure about the query and serviceInfo instructions, so if I'm wrong with the code, don't hesitate to point me in the right direction.
var azureMobileApps = require('azure-mobile-apps'),
promises = require('azure-mobile-apps/src/utilities/promises'),
logger = require('azure-mobile-apps/src/logger'),
queries = require('azure-mobile-apps/src/query');
var table = azureMobileApps.table();
table.insert(function (context) {
var query = queries.create('Services');
var serviceInfo = query.where({'Id': context.item.ServiceID});
var payload = '{"messageParam": "Your service -service name- has been added on ' + context.item.ServiceDetailDate + '" }';
return context.execute()
.then(function (results) {
if (context.push) {
context.push.send(null, payload, function (error) {
if (error) {
logger.error('Error while sending push notification: ', error);
} else {
logger.info('Push notification sent successfully!');
}
});
}
return results;
})
.catch(function (error) {
logger.error('Error while running context.execute: ', error);
});
});
module.exports = table;
Thanks for your help.
You can use the following code to query another easy table in ServiceDetails.js.
table.insert(function (context) {
return context.tables('Services')
.where({ Id: context.item.ServiceID })
.select('ServiceName')
.read()
.then(function (data) {
var message = 'Your service ' + data[0].ServiceName + ' has been added on ' + context.item.ServiceDetailDate;
var payload = '{"messageParam": "' + message + '"}';
return context.execute().then(function (results) {
//..
});
});
});

How do call a Meteor server from an Ionic app and have it respect your user ID

When I call try to call a Meteor server from an Ionic app, the user ID is not recognized on the server, despite having successfully logged in.
In the index.html of the Ionic app, before the meteor-client-side.bundle.js is included, the server URL is set:
SERVER_URL = "http://localhost:3000"; // Changes a lot. Run ipconfig. Look for Virtual Box???
window.__meteor_runtime_config__ = {
DDP_DEFAULT_CONNECTION_URL: SERVER_URL,
ACCOUNTS_CONNECTION_URL: SERVER_URL
};
Then, the user logins which is successful and I Meteor.user() looks good.
Meteor.loginWithPassword($scope.credentials.username,
$scope.credentials.password,
function (err) {
if (err) {
alert(err);
} else {
console.log('login success:' + JSON.stringify(Meteor.user()));
But, when the client code, tries to insert:
self.addOwnedProduct = function () {
var newOwnedProduct = {
productID: "ZZZZZZ" + Math.random(),
userID: Meteor.userId()
};
OwnedProducts.insert(newOwnedProduct);
};
The server code, sees the userId as null:
OwnedProducts = new Mongo.Collection("ownedProduct");
OwnedProducts.allow({
insert: function (userId, ownedProduct) {
console.log("Meteor.isServer:" + Meteor.isServer);
console.log("userId:" + userId);
return true || (userId && ownedProduct.userID === userId);
},
Here is a repo that replicates the problem
https://github.com/kokokenada/ionicToMeteorLogin

Meteorhack:Cluster subscription call failure

I have an UI app on port 8001 and an app named contract on port 7001. I have 'cluster' installed and working. I have a subscription and insert method defined in 'contract' app.
'contract' server/app.js
Cluster.connect("mongodb://<username>:<passwd>#URL");
var options = {
endpoint: "http://localhost:7001",
balancer: "http://localhost:7001", // optional
uiService: "web" // (optional) read to the end for more info
};
Cluster.register("contracts", options);
var Contracts = new Meteor.Collection('contracts');
Meteor.methods({
addContract: addContract,
findContracts: findContracts
});
Meteor.publish("getContracts", function () {
return Contracts.find({});
});
function addContract(c){
var data = {
id: c.id,
type: c.type
};
Contracts.insert(data);
}
function findContracts(){
var contracts = Contracts.find().fetch();
return contracts;
}
I am accessing the methods from an angular controller in my UI app.
UI app server/app.js
Cluster.connect(mongodb://<username>:<passwd>#URL");
var options = {
endpoint: "http://localhost:8001",
balancer: "http://localhost:8001" // optional
//uiService: "web" // (optional) read to the end for more info
};
Cluster.register("web", options);
Cluster.allowPublicAccess("contracts");
UI app controller code
var contractConn = Cluster.discoverConnection('contracts');
contractConn.subscribe('getContracts');
var SubscribedContracts = new Mongo.Collection('SubscribedContracts', {connection: contractConn});
console.log('status', contractConn.status());
vm.contracts = SubscribedContracts.find({}).count();
contractConn.call("findContracts", function(err, result) {
if(err) {
throw err ;
}
else {
console.log(result);
}
});
This is what is happening:
* I can access methods on the contract server
* I can insert or find contracts using these methods
* My subscription is not working. fetch on the cursor shows 0 and count shows 0
* Status on the connection shows 'connecting'
What am I doing wrong with my subscription?
Sudi
I had to change the name of the client mongo collection to the same name as the name of the collection on the service.

Firebase authentication on App initilisation

This is works:
console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
but this one is not:
$scope.authenticated.currentUser = user.id;
My goal here is to take to take some of the authentication variables (Email+UserID) and then use them to access a profile node ON firebase. On initialization I want the username, email, and a few other things I need for the app.
crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', function ($scope,defautProfileData,$q) {
var dataRef = new Firebase("https://glowing-fire-5401.firebaseIO.com");
$scope.authenticated={
currentUser: $scope.authemail,
emailAddress: "",
settings: "",
};
var chatRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com');
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
if (error) {
// an error occurred while attempting login
switch(error.code) {
case 'INVALID_EMAIL':
case 'INVALID_PASSWORD':
default:
}
} else if (user) {
// user authenticated with Firebase
console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
$scope.authenticated.currentUser = user.id ;//
} else {
// user is logged out
}
});
}]); //GlobaldCtrl
Most likely, you're running into a problem with Angular's HTML Compiler.
Whenever you use an event like ng-click/ng-submit/etc, Angular fires $scope.$apply(), which checks for any changes to your $scope variables and applies them to the DOM.
Since FirebaseSimpleLogin is not part of Angular's purview, it has no idea that when the callback is fired, you've updated $scope.authenticated.currentUser. This would also explain why it works when you call auth.login(), since you're probably invoking that via an ng-click event somewhere, which would fire a digest check and discover the changes.
If this is indeed the case, you can correct this issue by alerting Angular that it needs to run $apply by using $timeout:
crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', '$timeout', function ($scope,defautProfileData,$q, $timeout) {
/* ... */
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
if (error) {
/* ... */
} else if (user) {
$timeout(function() {
$scope.authenticated.currentUser = user.id ;//
});
} else {
// user is logged out
}
});

Using signalR to update Winforms UI

I have a form that was created on it's own UI thread running in the system tray which I need to manipulate with a signalR connection from the server which I believe to be running on a background thread. I'm aware of the need to invoke controls when not accessing them from their UI thread. I am able to manipulate (make popup in my case) using the following code that is called on form load but would like a sanity check as I'm fairly new to async:
private void WireUpTransport()
{
// connect up to the signalR server
var connection = new HubConnection("http://localhost:32957/");
var messageHub = connection.CreateProxy("message");
var uiThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var backgroundTask = connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("The connection was opened successfully");
}
});
// subscribe to the servers Broadcast method
messageHub.On<Domain.Message>("Broadcast", message =>
{
// do our work on the UI thread
var uiTask = backgroundTask.ContinueWith(t =>
{
popupNotifier.TitleText = message.Title + ", Priority: " + message.Priority.ToString();
popupNotifier.ContentText = message.Body;
popupNotifier.Popup();
}, uiThreadScheduler);
});
}
Does this look OK? It's working on my local machine but this has the potential to be rolled out on every user machine in our business and I need to get it right.
Technically you should hook up to all notifications (using On<T>) before you Start listening. As far as your async work I'm not quite sure what you were trying to do, but for some reason your chaining the notification to your UI in On<T> to the backgroundTask variable which is the Task that was returned to you by the call to Start. There's no reason for that to be involved there.
So this is probably what you want:
private void WireUpTransport()
{
// connect up to the signalR server
var connection = new HubConnection("http://localhost:32957/");
var messageHub = connection.CreateProxy("message");
var uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
// subscribe to the servers Broadcast method
messageHub.On<Domain.Message>("Broadcast", message =>
{
// do our work on the UI thread
Task.Factory.StartNew(
() =>
{
popupNotifier.TitleText = message.Title + ", Priority: " + message.Priority.ToString();
popupNotifier.ContentText = message.Body;
popupNotifier.Popup();
},
CancellationToken.None,
TaskCreationOptions.None,
uiTaskScheduler);
});
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("The connection was opened successfully");
}
});
}

Resources