How to redirect a user when the published cursor gets emptied? - redirect

I'm publishing a cursor that outputs just one document inside of it:
Meteor.publish ( 'test', function () {
return MyCollection.find ( 'DocumentMongoDBID' );
}
And I'm subscribing to it from the router (using iron router):
Router.route ( '/thepage', {
...
subscriptions: function () {
this.subscribe ( 'test' ).wait ();
}
}
I want to redirect the user to another page if the document gets deleted, hence if the cursor is empty.
How can I get a function invoked when this happens so that I can redirect the user? All the template's helpers that rely on that subscription gets called, but I'd prefer to have another function called before of them, that will eventually redirect the user.

Maybe try using LocalCursor.observeChanges on the client-side ?
Meteor.startup(() => {
MyCollection.find('DocumentMongoDBID').observeChanges({
removed(){
// redirect the user
Router.go(...);
}
});
});
https://docs.meteor.com/#/full/observe_changes

Related

FindOne never gets executed Meteor js

I have been on this for a while. The problem with is is that this line of code never get executed let userSchool = SchoolDb.findOne({slug: Session.get('ReceivedSlug')}); When I logged on the console I see the the slug is dynamic as it is suppose to be pull the record from the db. What am I to do right?
The oncreated template
Template.view.onCreated(function () {
Session.set('ReceivedSlug', FlowRouter.getParam('myslug'));
this.autorun(function () {
Meteor.subscribe('SingleSchool', Session.get('ReceivedSlug'));
});
});
The helper function
singleSchool: function () {
if (Meteor.userId()) {
console.log('reactive this ---- ' +Session.get('ReceivedSlug'));
let userSchool = SchoolDb.findOne({slug: Session.get('ReceivedSlug')});
if (!userSchool) {
Bert.alert('School not present', 'danger', 'growl-top-right');
} else {
console.log('school name ----' +userSchool.slug);
return userSchool;
}
}
},
Can you please check whether the subscription has fetched data. Also console out inside publish that whether data gets published when slug changed.
Use below code to check if subscription is working
Meteor.subscribe('SingleSchool', Session.get('ReceivedSlug'), {
onReady: function(){
console.log(SchoolDb.find({}).fetch());
}
});

Magento 2 Place order before sending to Payment Gateway?

I am developing a Payment Gateway which the payment will be done by redirecting the browser to the bank website and payment would be done there and after a success it will be redirected back to the website
I could be able to redirect using the JavaScript code in payment renderer successfully but the problem is that the order will not be placed before the redirection to the bank
How to make the order and get the Order_ID before sending the user to the bank
The following is my js Code that redirects the user to a sepecific controller that handles the redirection
continueToMellat: function () {
//update payment method information if additional data was changed
$.mage.redirect(url.build('redirect/redirect'));
this.selectPaymentMethod();
setPaymentMethodAction(this.messageContainer);
return false;
}
Thanks
I could be able to place order before redirecting by changing the code to the following, this will place the order before sending to the payment gateway
I am now trying to get the order id of which the user has just ordered
continueToMellat: function () {
if (this.validate() && additionalValidators.validate()) {
this.isPlaceOrderActionAllowed(false);
this.getPlaceOrderDeferredObject()
.fail(
function () {
self.isPlaceOrderActionAllowed(true);
}
).done(
function () {
self.afterPlaceOrder();
if (self.redirectAfterPlaceOrder) {
redirectOnSuccessAction.execute();
}
}
);
}
//update payment method information if additional data was changed
$.mage.redirect(url.build('redirect/redirect'));
this.selectPaymentMethod();
setPaymentMethodAction(this.messageContainer);
return false;
}
You could maybe use the cart ID : getQuoteId() like used in
//vendor/magento/module-checkout/view/frontend/web/js/action/get-payment-information.js
define([
'jquery',
'Magento_Checkout/js/model/quote',
...
], function ($, quote, ...) {
'use strict';
return function (deferred, messageContainer) {
var serviceUrl;
deferred = deferred || $.Deferred();
/**
* Checkout for guest and registered customer.
*/
if (!customer.isLoggedIn()) {
serviceUrl = urlBuilder.createUrl('/guest-carts/:cartId/payment-information', {
cartId: quote.getQuoteId()
});
}
though the order increment Id can be reserved in server before passing to client processing :
__construct(\Magento\Checkout\Model\Session $checkoutSession){
$this->quote = $checkoutSession->getQuote();
}
...
$this->quote->reserveOrderId();
$orderId = $this->quote->getReservedOrderId();

Slim route groups not working as expected

In my code I am doing something very similar to the following (from the slim docs). My expected behaviour is that groups restrict the route scope to within that group e.g. /library/books is not the same as /books. However, I'm finding in my code that the group method is not restricting the route as expected and for example the route for /admin/tours is being called even when I go to /tours. Is there something I am missing? The docs for group on the homepage (www.slimframework.com) of the slim website are different from the documentation website (http://docs.slimframework.com/routing/groups/).
$app = new \Slim\Slim();
// API group
$app->group('/api', function () use ($app) {
// Library group
$app->group('/library', function () use ($app) {
// Get book with ID
$app->get('/books/:id', function ($id) {
});
// Update book with ID
$app->put('/books/:id', function ($id) {
});
// Delete book with ID
$app->delete('/books/:id', function ($id) {
});
});
});
Further example
$app->group( '/admin', function () use ( $app , $twig) {
$app->get('/tours', function() use ($app){
print_r('do tours admin');
});
});
$app->get('/tours', function() use ($app){
print_r('do tours');
});
my behaviour is that /tours is still routing to /admin/tours
It seems like a bit of a simple solution but ordering the registering of the /tours route before the /admin/tours route sorted it.

How to get logged off users email address in meteor?

In my routing file I have the following down.
Router.route('/user/:createdBy', {
name: 'user',
/*onBeforeAction: function () {
AccountsEntry.signInRequired(this);
},*/
fastRender: true,
data: function () {
paramId = this.params.createdBy;
// Still have to find a way how to get data
// Function below is only for signed in users
return Meteor.users.findOne(paramId);
}
});
In my user template I want to display the email. I have it like this {{emails.[0].address}} and as {{users.emails.[0].address}} but the email doesn't show up. It only shows up if the user is logged in. I however have the users Id as my param. (This is for testing purposes guys!).
If you want to use the logged off user information, you could try this:
// in your router.js
// callback would be called when login is successful
Meteor.onLogin(function(){
Session.set('login user', Meteor.user());
})
// in your template, or other functions
// the Session would be update only if a user login successfully
var userId = Session.get('login user')._id;
Click here for more details.
I wish it could help :-)

Autopublish removed but why can I still retrieve data from db?

I have a simple Meteor/MongoDB project using the 'roles' package where I optain data from the db to the client. The roles package seems to work fine and the browser shows the right data depending on who is logged in, just like it should do. Then when running 'meteor remove autopublish' in the terminal inside my applications directory I get 'autopublish removed' just like it should. Still I can retrieve data from the server just as before(!?)
I have all of my db calls from the client/client.js.
The server/server.js does nothing (I do have publish/subscribe code but uncomment for now) and same goes for the common js file in main directory.
How can this be? Am I perhaps retrieving data from minimongo somehow? I have also removed insecure even if I don't think that matters in this case(?) Thanks in advance.
EDIT: Here's the code:
client.js:
//when uncomment the subscribe's you should not get access to the server/db, but 'data' that holds all the inlogg info still shows. The 'movies' on the other hand doesn't, just like it shouldn't.
//Meteor.subscribe('data');
//Meteor.subscribe('movies');
/*############# Get User Data ###############*/
Template.userLoggedIn.id = function () {
return Meteor.userId();
};
Template.userLoggedIn.email = function () {
var email = Meteor.users.findOne({_id: Meteor.userId()});
return email.emails[0].address;
};
Template.userLoggedIn.profile = function () {
var profile = Meteor.users.findOne({_id: Meteor.userId()});
return profile.profile.name;
};
Template.userLoggedIn.role = function () {
var role = Meteor.users.findOne({_id: Meteor.userId()});
return role.roles[0];
};
/*############# ###############*/
Template.movies.movies = function() {
var movies = Movies.find().fetch();
return movies;
}
server.js:
Meteor.publish('data', function () {
return Meteor.users.find();
});
Meteor.publish('movies', function() {
return Movies.find();
});
Thanks for providing the code - I see how this could be confusing. The users section of the docs should be written to explicitly say this, but what's happening is the current user is always published. So even if you don't write a publish function for users (or your have your subscribe commented out), you should expect to see the current user on the client. Because your template code only looks for Meteor.userId(), I would expect it to still work.
Assuming you have other users in the database, you can quickly check that they are not being published by running: Meteor.users.find().count() in your browser console. If it returns 1 then you are only publishing the current user (or 0 if you are logged out).