Hi All,
I relatively new to meteor so still getting to grips with all the publish/subscribe and the reactivity bits.
The scenario:
- A user has a board which they can add and remove items to and from.
- This all connects to an external API I’ve developed - this is all working.
- I’m connecting this all together using publish/subscribe - this is all working.
- Now this is where I run into a bit of trouble on how to achieve what I’m after. I want to update the board without the need to refresh the page as this is the only way I’m able to see the newly added/removed items from the board.
This is my code:
Helpers:
Products = new Mongo.Collection('products');
Tracker.autorun(function () {
Meteor.subscribe("getProducts");
});
Template.productsList.helpers({
products(){
return Products && Products.find();
}
});
Server side
Meteor.publish('getProducts', function () {
const self = this;
try {
const response = HTTP.get(MyAPICall);
const boardData = response.data.board[0];
_.each(boardData.products, function (item) {
var doc = {
boardId: boardData._id,
id: item._id,
name: item.name,
url: item.url,
image: item.imagePath,
price: Number(item.price).toFixed(2),
valueChange: item.valueChanged,
percentageChange: item.percentageChanged,
dateAdded: item.createdAt
};
self.added('products', item._id, doc);
});
self.ready();
} catch (error) {
console.log(error);
}
});
Events:
Meteor.call('trackProduct', Session.get('productData'), targetPrice, (error, results) => {
if (error) {
throwError('trackProduct event', error);
} else {
document.location.reload(true); // This is what I need to do to see the changes.
}
});
The issue is probably something so simple I’ve missed it in the docs, but googling it I’ve still not managed to find a solutions.
Any help/advice would be greatly appreciated.
Thanks in advance.