I have been trying to get server side account user creating to work but I have come across an issue with the check() method I am using server side. (I am using simple-schema for this)
When the password is empty, this causes check() to throw an error, and rightly so. However, this is a server-side error and I am not quite sure how to propagate this to the client to be caught and dealth with.
The exception that I can see from my browser console is as follows:
Exception while simulating the effect of invoking 'createUserAccount' Meteor.makeErrorType.errorClass {message: "Match error: One or more properties do not match the schema.", path: "", sanitizedError: Meteor.makeErrorType.errorClass, errorType: "Match.Error", stack: (...)…} Error: Match error: One or more properties do not match the schema.
at SimpleSchema.condition (http://localhost:3000/packages/aldeed_simple-schema.js?8fda161c43c0ba62801a10b0dfcc3eab75c6db88:2450:11)
at checkSubtree (http://localhost:3000/packages/check.js?ac81167b8513b85b926c167bba423981b0c4cf9c:255:17)
at check (http://localhost:3000/packages/check.js?ac81167b8513b85b926c167bba423981b0c4cf9c:67:5)
at Meteor.methods.createUserAccount (http://localhost:3000/both/methods/accounts.js?c418120e76666f0ca774a281caafc39bc2c3a59d:4:27)
at http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4244:25
at _.extend.withValue (http://localhost:3000/packages/meteor.js?81e2f06cff198adaa81b3bc09fc4f3728b7370ec:949:17)
at _.extend.apply (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4235:54)
at _.extend.call (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4113:17)
at Object.Template.PasswordRegister.events.submit form (http://localhost:3000/client/views/shared/accounts/accounts.js?ac573d92938a2b3d6107ea19e50065f7ac5d41b3:36:20)
at null. (http://localhost:3000/packages/blaze.js?efa68f65e67544b5a05509804bf97e2c91ce75eb:3147:18)
Here is how my client code looks like :
Template.PasswordRegister.events({
'submit form': function(event, template) {
event.preventDefault();
var user = {
email: template.find('#email').value,
password: template.find('#password').value
};
Meteor.call('createUserAccount', user, function(error) {
if (error) {
console.log("CONSOLE : " + error);
//TODO DO SOMETHING
// return alert(error.reason);
} else {
Meteor.loginWithPassword(user.email, user.password, function(error) {
if (error) {
console.log("CONSOLE : " + error);
//TODO DO SOMETHING
// return alert(error.reason);
}
});
}
});
}
});
and here is my server side code:
Meteor.methods({
createUserAccount: function(user) {
// Important server-side check for security and data integrity
check(user, Schema.registration);
var email = user.email;
var password = user.password;
this.unblock();
return Accounts.createUser({
email: email,
password: password
});
}
});
I've tried wrapping client-side code with a normal try catch block but didn't make any difference; that console error still shows.
As the error message says, you have a method stub for 'createUserAccount' defined on the client. It is that client stub that is throwing the exception.
Wrap the method shown with if (Meteor.isServer) to keep it from running on the client.
if (Meteor.isServer ){
Meteor.methods({
createUserAccount: function (user) { ... }
});
}
If that doesn't work search your project for the client code defining the method stub.
To clarify what is happening I have made a meteorpad with a method incorrectly stubbed on the client that throws the error you see in the browser console. I have then added a second method, 'creatUserAccount1', which is only defined on server. When this second method is called, its error is handled by callback and does not cause an exception. I think that is the behaviour you want.
Related
I have used the Meteor Ionic Demo code to create my new application. Now, instead of signing up with an e-mail address, I want to use usernames.
I used this code (source):
// server/methods.js
if (Meteor.isServer){
Meteor.methods({
"userExists": function(username){
return !!Meteor.users.findOne({username: username});
},
});
}
// lib/config/at_config.js
AccountsTemplates.addField({
_id: 'username',
type: 'text',
required: true,
func: function(value){
if (Meteor.isClient) {
console.log("Validating username...");
var self = this;
Meteor.call("userExists", value, function(err, userExists){
if (!userExists)
self.setSuccess();
else
self.setError(userExists);
self.setValidating(false);
});
return;
}
// Server
return Meteor.call("userExists", value);
},
errStr: "Bad username"
});
Now the problem is, if the username already exists, how can I display an error message?
Currently, it displays only the ionic error icon, but not the errStr:
I think the problem is that I should return userExists in func, but how can I wait on the userExists method server call?
The function passed to Meteor.call("userExists", value, function(err, userExists){ is the callback that will be called when the server method returns a response, so you are already waiting for it to finish.
From the source code of meteor-useraccounts/core, it seems that errStr is only used when func returns true. In your case, because the call to server is async, func is returning undefined which is considered 'no error'.
Also, what is shown next to the icon is the status of the field, and that status is modified by setError.
If you pass the error message to setError, it will be displayed correctly.
The solution is to change:
self.setError(userExists);
to:
self.setError("Bad username");
The user will click on a button that will invoke the Parse Cloud function sendText()
I've tried both Live Twilio and Testing Twilio accSID and authToken
I first initialize my Twilio by:
var Twilio = require('twilio');
Twilio.initialize('accountSid', 'authToken'); //put in my corresponding <<
then I set the Parse function by:
Parse.Cloud.define('sendText', function(request, response) {
Twilio.sendSMS({
From: '+1234567890', //From Number
To: "+0987654321", //To Number
Body: "Start using Parse and Twilio!" //Message <<
}, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
}
It would be great to have someone tell me if something here is wrong or if there are other approaches in sending SMS through Twilio via Parse Cloud.
On the SMS Summary on Twilio, it does not even know any SMS being sent out.
Going on...
The button that calls this cloud function is:
<button type="button" class="page-scroll btn btn-xl" onclick="saveData()">CONFIRM</button>
and the js function that is called saveData() is:
function saveData() {
booking.save({
something: something,
}, {
success: function (booking) {
window.location.href = 'final.php';
Parse.Cloud.run('sendText',
{
something: something
});
},
error: function (booking, error) {
alert('Failed to save');
}
});
}
NO ERROR LOG
Twilio developer evangelist here.
You seem to be using an old Parse module which is no longer supported by us. The new module however uses a newer version of our Node module.
Some documentation for it can be found here
It also has some sample code to do what you're trying to do.
// Require and initialize the Twilio module with your credentials
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');
// Send an SMS message
client.sendSms({
to:'+0987654321',
from: '+1234567890',
body: 'Hello world!'
}, function(err, responseData) {
if (err) {
console.log(err);
} else {
console.log(responseData.from);
console.log(responseData.body);
}
}
);
I think you will find your SMS will be sent using this version of the code. Notice how the initialization is different.
Using the service-configuration and accounts-facebook packages, after clicking on the facebook button and logging in from the Facebook authorization window that pops up, we're getting an Internal server error when performing a Meteor.loginWithFacebook.
This was tested on a very basic example, what is causing this error?
Template.login.events({
'click .btn-facebook': function (ev) {
Meteor.loginWithFacebook({}, function(error) {
if(error) {
throw new Meteor.Error('Facebook login failed: ', error);
}
})
}
});
/server/lib/config/social.js
Meteor.startup(function() {
ServiceConfiguration.configurations.update(
{ service: "facebook" },
{ $set: {
appId: "xxx",
secret: "xxx"
}
},
{ upsert: true }
);
})
Error (server side)
Exception while invoking method 'login' undefined
Error (client side)
Error: Internal server error [500]
at _.extend._livedata_result (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4964:23)
at onMessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:3725:12)
at http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:2717:11
at Array.forEach (native)
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:11)
at _.extend._launchConnection.self.socket.onmessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:2716:11)
at REventTarget.dispatchEvent (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:156:22)
at SockJS._dispatchMessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:1141:10)
at SockJS._didMessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:1199:18)
at WebSocket.SockJS.websocket.that.ws.onmessage (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:1346:17)
I had the same issue, and solved it like this :
After checking that you have configured your schema as described here :
Schema.User = new SimpleSchema({
_id: {
type: String
}
...
});
You should add the second part into an Accounts.onCreateUser like this, into server/accounts.js for example :
Accounts.onCreateUser(function (options, user) {
if (user.services.facebook) {
user.emails = [{
address: user.services.facebook.email,
verified: true
}];
}
return user;
});
It will append the facebook email to the newly created account. The error should disapear.
I'm looking for a way to determine if Meteor.user() is set in a function that can be called both from the server and client side, without raising an error when it is not.
In my specific case I use Meteor server's startup function to create some dummy data if none is set. Furthermore I use the Collection2-package's autoValue -functions to create some default attributes based on the currently logged in user's profile, if they are available.
So I have this in server-only code:
Meteor.startup(function() {
if (Tags.find().fetch().length === 0) {
Tags.insert({name: "Default tag"});
}
});
And in Tags-collection's schema:
creatorName: {
type: String,
optional: true,
autoValue: function() {
if (Meteor.user() && Meteor.user().profile.name)
return Meteor.user().profile.name;
return undefined;
}
}
Now when starting the server, if no tags exist, an error is thrown: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
So in other words calling Meteor.user() on the server startup throws an error instead of returning undefined or null or something. Is there a way to determine whether it will do so prior to calling it?
I cannot solve this simply by wrapping the call with if (Meteor.isServer) within the autoValue function, as the autoValue functions are normally called from server side even when invoked by the user, and in these cases everything in my code works fine.
Note that this is related to How to get Meteor.user() to return on the server side?, but that does not address checking if Meteor.user() is available in cases where calling it might or might not result in an error.
On the server, Meteor.users can only be invoked within the context of a method. So it makes sense that it won't work in Meteor.startup. The warning message is, unfortunately, not very helpful. You have two options:
try/catch
You can modify your autoValue to catch the error if it's called from the wrong context:
autoValue: function() {
try {
var name = Meteor.user().profile.name;
return name;
} catch (_error) {
return undefined;
}
}
I suppose this makes sense if undefined is an acceptable name in your dummy data.
Skip generating automatic values
Because you know this autoValue will always fail (and even if it didn't, it won't add a useful value), you could skip generating automatic values for those inserts. If you need a real name for the creator, you could pick a random value from your existing database (assuming you had already populated some users).
Been stuck with this for two days, this is what finally got mine working:
Solution: Use a server-side session to get the userId to prevent
"Meteor.userId can only be invoked in method calls. Use this.userId in publish functions."
error since using this.userId returns null.
lib/schemas/schema_doc.js
//automatically appended to other schemas to prevent repetition
Schemas.Doc = new SimpleSchema({
createdBy: {
type: String,
autoValue: function () {
var userId = '';
try {
userId = Meteor.userId();
} catch (error) {
if (is.existy(ServerSession.get('documentOwner'))) {
userId = ServerSession.get('documentOwner');
} else {
userId = 'undefined';
}
}
if (this.isInsert) {
return userId;
} else if (this.isUpsert) {
return {$setOnInsert: userId};
} else {
this.unset();
}
},
denyUpdate: true
},
// Force value to be current date (on server) upon insert
// and prevent updates thereafter.
createdAt: {
type: Date,
autoValue: function () {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset();
}
},
denyUpdate: true
},
//other fields here...
});
server/methods.js
Meteor.methods({
createPlant: function () {
ServerSession.set('documentOwner', documentOwner);
var insertFieldOptions = {
'name' : name,
'type' : type
};
Plants.insert(insertFieldOptions);
},
//other methods here...
});
Note that I'm using the ff:
https://github.com/matteodem/meteor-server-session/ (for
ServerSession)
http://arasatasaygin.github.io/is.js/ (for is.existy)
Just hit an insanely frustrating roadblock in prototyping. I need to update and increment values an array inside of a collection. To do this, I'm accessing the collection using the MongoDB syntax like so:
Players.update({_id: Session.get('p1_id'), 'opponents.$.id' : Session.get('p2_id')},
{$inc: {
'games_played' : 1
}}
);
When this runs I get an error saying: Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]
Now, I searched the hell out of this and I know that it came down in an update and why they only allow update by id's. But my problem is that I can't seem to find a way around it. I tried forcing it by adding this to if (Meteor.isServer):
Players.allow({
insert: function(userId, doc, fields, modifier){
return true;
},
update: function(userId, doc, fields, modifier){
return true;
},
remove: function(userId, doc, fields, modifier){
return true;
}
});
Nothing seems to work, and all the examples I find talk about using a Meteor method (not really sure what that is) or are doing userId validation (I dont have any users and don't want to add them right now). I'm just prototyping/sketching and I'm not concerned about security. How can I proceed here?
Here's how you can make this into a method:
Meteor.methods({
incrementGames: function (player1Id, player2Id) {
check(player1Id, Meteor.Collection.ObjectID);
check(player2Id, Meteor.Collection.ObjectID);
Players.update({
_id: player1Id,
'opponents.$.id': player2Id
}, {
$inc: {
'games_played' : 1
}
}, function(error, affectedDocs) {
if (error) {
throw new Meteor.Error(500, error.message);
} else {
return "Update Successful";
}
});
}
});
And on your client:
Meteor.call("incrementGames", Session.get('p1_id'), Session.get('p2_id'), function(error, affectedDocs) {
if (error) {
console.log(error.message);
} else {
// Do whatever
}
});
You just got the update wrong. The first parameter of the update method should be the id. the second parameter is an object containing the modifiers.
Players.update(playerId, {$inc:{games_played:1}});
Optionally you can add a callback containing error as the first parameter and response as the second parameter.