0

In my Meteor app, I have a simple array field called relatedSentences. It is defined using SimpleSchema

 relatedSentences: {
     type: [String],
     label: "Related Sentence",
     optional: false,
     defaultValue: [] 
 },

It's data can be seen in the Mongo console:

"_id" : "ei96FFfFdmhPXxRWb",
"sentence" : "the newest one",
"relatedSentences" : [
    "Ls6EyBkbcotcyfLyw",
    "6HKQroCjZhG9YCuBt"
],
"createdAt" : ISODate("2015-10-25T11:21:25.338Z"),
"updatedAt" : ISODate("2015-10-25T11:41:39.691Z")

But when I try to access this field using this, it is returned as a raw string.

Template.showSentence.helpers({
   translations: function() {
      console.log("related: " + this.relatedSentences);
      Meteor.subscribe('sentences', function() {
        var relatedSentences = Sentences.find({_id: {$in: this.relatedSentences} }).fetch();
        console.log("rel count" + relatedSentences.length);
        return relatedSentences;
      }); 
   }
});

In the console I get an error. See the return value from the this.relatedSentences. It is the contents of the array as a string, with a comma interpolated.

related: Ls6EyBkbcotcyfLyw,6HKQroCjZhG9YCuBt
selector.js:595 Uncaught Error: $in needs an array

Not sure what is going on here.

Some Progress

I have made some progress, but not yet at a solution. By adding blackbox: true to the SimpleSchema definition, what looks like an array is now returned... but alas it is still failing. See below.

  relatedSentences: {
     type: [String],
     label: "Related Sentence",
     optional: false,
     blackbox: true,
     defaultValue: []
  },

Now I get the results below in the console. The values are now being returned as a quoted array, which is what I expected. But the $in is still not seeing it as an array.

["Ls6EyBkbcotcyfLyw", "6HKQroCjZhG9YCuBt"]
selector.js:595 Uncaught Error: $in needs an array

How did the data get populated

In answer to @Kyll - this is how the data was originally populated. I am using AutoForm,

     {{> afQuickField name='relatedSentences.0' value=this._id type="hidden"}}

but then adding the array data via a hook.

AutoForm.addHooks('translateForm', {
   onSuccess: function (operation, result, template) {
      Meteor.subscribe('sentences', function() {
        var translatedSentence = Sentences.findOne(result);
        var originalSentenceId = translatedSentence.relatedSentences[0]
        Sentences.update(
           { _id: originalSentenceId}, 
           { $push: { relatedSentences: result}
        });
        Router.go('showSentence',{ _id: originalSentenceId } );
      });     
   }
});
6
  • realated should look like ['Ls6EyBkbcotcyfLyw','6HKQroCjZhG9YCuBt'] inside array not a string. The error tells you the same thing. $in accepts arrays only. Commented Oct 25, 2015 at 12:21
  • Yes I understand the error message, but I don't understand why Meteor is returning the array as a string Commented Oct 25, 2015 at 12:24
  • How do you pass the template data? Commented Oct 25, 2015 at 12:53
  • Thanks @Kyll I'm not sure what you mean. The template is showSentence and the helper can access this and the relatedSentence field. Commented Oct 25, 2015 at 12:59
  • Yes, but apparently your data somehow got skewed into a String on the way there. So how did it get into your template data? Through a router? A handlebars call..? Commented Oct 25, 2015 at 13:00

1 Answer 1

1

The problem here is the scope of this. You are referring to it inside the subscribe function, where this has a different context. Set a varibale to this in the context where it works, then use that variable instead:

Template.showSentence.helpers({
   translations: function() {
      console.log("related: " + this.relatedSentences);
      var this_related_sentences = this.relatedSentences;
      Meteor.subscribe('sentences', function() {
        var relatedSentences = Sentences.find({_id: {$in: this_related_sentences} }).fetch();
        console.log("rel count" + relatedSentences.length);
        return relatedSentences;
      }); 
   }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.