1

I'am working on my domain-generating app which provides domain names in different languages.

is it possible to store a mongo collection inside user sessions? So every user could see different collection results. I have a server side api call which data goes into mongo collection:

Meteor.methods({
  translateToEn: function(){
    var arrayy = Help.find().fetch();
    var text = arrayy[0].text;
   var final;
   var myJSON;

    Typed.remove({});

    var translate = require('yandex-translate-api')('trnsl.1.1.20170424T153548Z.48f645437a55346f.e53fc386af70fd7d7f7138ae23b6c79fb3a5def9');
    translate.translate(text, { to: 'en'},Meteor.bindEnvironment( function(err, res) {

    myJSON = JSON.stringify(res.text);
    myJSON = myJSON.replace(/\s/g, '');
    myJSON = JSON.parse(myJSON);
    myJSON.toString()
    final = myJSON;
   var geras = final.toString();
   var naujas = Diacritics.remove(geras);

     final = naujas;

    var array = [{"text": " "}]
    var ends = [{"text": ".com"},{"text": ".co.uk"},{"text": ".net"},{"text": ".org"},{"text": ".eu"},{"text": ".biz"},{"text": ".blog"},{"text": ".in"}]
    var sug;

    for(var i=0; i<8; i++)
    {
        var randomIndex = Math.floor( Math.random() * array.length );
        var randomIndex2 = Math.floor( Math.random() * ends.length );
        var element = array[randomIndex].text;
        var end = ends[randomIndex2].text;
         sug = final + element+end;
         sug = sug.replace(/\s/g, '');
         Typed.insert({
         text: sug,
         createdAt: new Date(), 
         lang: res.lang });

    }

url = 'https://api.ote-godaddy.com/v1/domains/available?checkType=FAST';

headers = {
    "Authorization": "sso-key 2s7YSCfHkx_Xsfgx2tB1fV4WVrdd8VQuz:XsfkhRdwfMCm633B7GT6qz",
    "Content-Type": "application/json",
    "Accept": "application/json"
}

var arrays = Typed.find().fetch();
var test = arrays[0].text+' ';
var lang = arrays[0].lang;
console.log(lang);
for(var i = 1; i<arrays.length-1; i++)
{
 test = test + arrays[i].text+' ';
}
test=test+arrays[arrays.length-1].text;
logs = test.split(" ");
//console.log(logs);

HTTP.post(url,{
    data: logs,
    headers : headers
}, function( error, response ) {

  if ( error ) {
    console.log( error );
  } else {
   var results = JSON.parse(response.content);

  for(i =0; i< results.domains.length; i++){
     if(results.domains[i].price == null)
     {
       results.domains[i].price = "undefined";
     }
     if(results.domains[i].available == false)
     {
       results.domains[i].available = "false";
       TransToEnF.insert({domain: results.domains[i].domain, available: results.domains[i].available,definitive: results.domains[i].definitive,price: results.domains[i].price,currency: results.domains[i].currency, Buy:"Buy", lang:lang, translation:final});
     }
      if(results.domains[i].available == true)
     {
       results.domains[i].available = "true";
       TransToEnT.insert({domain: results.domains[i].domain, available: results.domains[i].available,definitive: results.domains[i].definitive,price: results.domains[i].price,currency: results.domains[i].currency, Buy:"Buy", lang:lang, translation:final});
     }
  }
  }
});
Typed.remove({});
}));
}});

I'm calling this method from client-side submit event like this:

Template.search.events({
  'submit .new-input'(event) {

    Meteor.call('remove',function(){});
    event.preventDefault();


    const target = event.target;
    const text = target.text.value;
    Help.insert({text: text});
    Place.insert({text: text,createdAt: new Date()});
    Meteor.call('translateToEn',function(){});

    target.text.value = '';
Meteor._reload.reload();
  },

and printing data using helpers:

//available domains
Template.search.helpers({
  transToEnT: function(){
  return Session.get(TransToEnT.find()) ;
  }
});

Template.search.helpers({     //taken domains
  transToEnF: function(){
  return TransToEnF.find();
  }
});

unfortunately, if there are more than 1 user, they all see the same results, page reloads imediatly after one of them submits the text field. Do you guys have any solutions to solve this problem?

1 Answers1

0

Looking at your code I could see that the translateToEn is called by anonymous users. Correct me if I'm wrong.

If that is the case what I would suggest is to generate some kind of random key on client and attach your data with that key.

So on client you can store a token in cookie. You can use Meteor's Random package to generate random keys var token = Random.id() or you can also use Meteor.uuid(). You can set cookie expiry as Session which is the default check this Session only cookies with Javascript.

Finally you can change translateToEn to accept token parameter and then store documents with this token

Meteor.methods({
  translateToEn: function(token){
    check(token, String);

    // 
    // do something and get result
    // 

    TransToEnF.insert({ ... , token: token});

pass the same token in a publish function to get the filtered result

 Meteor.publish("translateToEn", function(token) {
  check(token, String);
  // send only filtered results;
  return [ TransToEnF.find({token: token}), TransToEnT.find({token: token })]
});

If the user is logged in you should check for userId in the method and publish function this.userId should be defined. For session you can use the hashedToken which is attached to the this.connection. You need to use accounts-base package for that.

The token for connection can be retrieved using

var token = Accounts._getLoginToken(this.connection.id);

Hope this helps

dc4ual
  • 95
  • 9