Hi guys I am trying to update my collections from the client thus I use Meteor method and call. Here is my code of server main.js
import { Meteor } from 'meteor/meteor';
import '../imports/api/tasks.js';
import { Conns } from '../imports/api/conns.js';
import { Ips } from '../imports/api/ips.js';
Meteor.startup(() => {
// code to run on server at startup
Meteor.onConnection(function(conn){
var connID = conn.id ;
var ipAdr = conn.clientAddress ;
var realIP = conn.httpHeaders['x-real-ip'] ;
console.log(connID);
// console.log(this.connection.id) this returns error
//trying to send the connID to the client
// Meteor.methods({
// getSessionId: function() {
// return connID;
// }
// });
//checking whether we are getting the right ips
if (ipAdr !== realIP) {
if (!realIP) {
console.log('You are running locally');
}
else {
console.log('You forget HTTP_FORWARDED_COUNT="1" or put the wrong number of proxies');
}
}
//check if the ip is registered
// console.log(Ips.findOne({"ipAdr":ipAdr}));
if (!Ips.findOne({"ipAdr":ipAdr})) {
Ips.insert({
ipAdr:ipAdr,
connections: [{
connID: conn.id,
ipAdr: conn.clientAddress,
httpHeads: {
host: conn.httpHeaders.host,
userAgent:conn.httpHeaders['user-agent'],
realIP: conn.httpHeaders['x-real-ip'],
},//httpHeads
connectedAt: new Date(),
disconnectedAt: null,
clicks: Array(),
}],//connections
createdAt: new Date(),
});//Ips.insert
//
// console.log(); console.log();
}
else {
Ips.update({"ipAdr":ipAdr}, {$push:{
'connections':{
connID: conn.id,
ipAdr: conn.clientAddress,
httpHeads: {
host: conn.httpHeaders.host,
userAgent:conn.httpHeaders['user-agent'],
realIP: conn.httpHeaders['x-real-ip'],
},
connectedAt: new Date(),
disconnectedAt: null,
clicks: Array(),
}
}});
}//else
conn.onClose(function () {
console.log('connection closed');
//findAndModify will always refer to one document
Ips.findAndModify({
//Find the desired document based on specified criteria
query: { "ipAdr": ipAdr, connections: { $elemMatch: { connID: conn.id}}},
//Update only the elements of the array where the specified criteria matches
update: { $set: { 'connections.$.disconnectedAt': new Date()}}
});
});//onClose
});// Meteor onConnection
});//Meteor startup
Meteor.methods({
'updateDB':function({clientIp,clientConnId}){
// Ips.update(
// { "ipAdr": clientIp, "connections.connID": clientConnId},
// { "$push":
// {"connections.$.clicks":
// {
// 'clickedThis': clickedOne, 'clickedAt': new Date(),
// }
// }
// }
// )
Ips.findAndModify({
//Find the desired document based on specified criteria
query: { "ipAdr": clientIp, connections: { $elemMatch: { connID: clientConnId}}},
//Update only the elements of the array where the specified criteria matches
update: { $push: { 'connections.$.clicks': {clickedThis: clickedOne, clickedAt: new Date()} }}
});
}
});
As you can see in my call I tried to update my db by 2 ways but both give me the same 500 error. I have a package which adds support for findAndModify.
And here is my client code
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Tasks } from '../api/tasks.js';
import { Conns } from '../api/conns.js';
import { Ips } from '../api/ips.js';
import "./body.html";
Template.body.helpers({
tasks(){
return Tasks.find({});
},
conns(){
return Conns.find({},{ sort: { createdAt: -1 } });
},
ips(){
// console.log(Ips.find({},{sort: { createdAt: -1 } }).fetch());
return Ips.find({},{sort: { createdAt: -1 } });
// XXX: //in stack overflow they say in order to show the
//whole collection as a string one should use publish and subscribe
// be sure to check that
},
});
Template.registerHelper("keyval",function(object){
return _.map(object, function(value, key) {
return {
key: key,
value: value
};
});
});
// Template.ip.onCreated(function () {
// console.log('created');
// $(function() {
// $(document).ready(function() {
// $('.conns').hide();
// });
//
// });
// });
Template.ip.events({
"click .showHide": function(event, template){
console.log('I log');
// console.log($(this));// this is the whole template
console.log(event.target);
$(function() {
// your jQuery code here...
// console.log($(event.target));
// console.log($(event.target).parent());
// console.log($(event.target).parent().next('conns'));
// console.log($(event.target).parent().next('conns').first());
// console.log($(event.target).siblings('conns'));
$(event.target).next('.conns').toggle(500); //event.target is the event emitter
});
}//.showHide clicks
});//Template.ip.events
Template.body.events({
"click *": function(event, template){
event.stopPropagation();
console.log('body all click log');
var clickedOne = $(event.target).html().toString();
console.log('This click ' + clickedOne);
//getting the connID
// Meteor.call("getSessionId", function(err, id) {
// return console.log(id);
// });
var clientIp = headers.getClientIP();
var clientConnId = Meteor.connection._lastSessionId;
console.log(clientIp);
console.log(clientConnId);
// Ips.findAndModify({
//
// //Find the desired document based on specified criteria
// query: { "ipAdr": clientIp, connections: { $elemMatch: { connID: clientConnId}}},
//
// //Update only the elements of the array where the specified criteria matches
// update: { $push: { 'connections.$.clicks': {clickedThis: clickedOne, clickedAt: new Date()} }}
// });
Meteor.call("updateDB", {clientIp,clientConnId}, function(error, result){
if(error){
console.log("error", error);
}
if(result){
}
});
}
});
// Template.ip.onCreated(function functionName() {
// (function ($) {
// $('.showHide').click(function () {
// console.log('I log');
// $(this).parent().next('conns').first().toggle();
// });
// })(jQuery);
// });
And here is the full project in case you need it
Please help me to understand and solve this problem.
Thank you in advance.