【发布时间】:2015-10-27 05:20:51
【问题描述】:
在我的 Meteor 应用程序中,我有一个名为 relatedSentences 的简单数组字段。它是使用 SimpleSchema 定义的
relatedSentences: {
type: [String],
label: "Related Sentence",
optional: false,
defaultValue: []
},
它的数据可以在 Mongo 控制台中看到:
"_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")
但是当我尝试使用 this 访问该字段时,它会作为原始字符串返回。
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;
});
}
});
在控制台中出现错误。查看 this.relatedSentences 的返回值。它是将数组的内容作为字符串,插入逗号。
related: Ls6EyBkbcotcyfLyw,6HKQroCjZhG9YCuBt
selector.js:595 Uncaught Error: $in needs an array
不知道这里发生了什么。
一些进展
我已经取得了一些进展,但还没有找到解决方案。通过将 blackbox: true 添加到 SimpleSchema 定义中,现在返回了看起来像一个数组的内容......但可惜它仍然失败。见下文。
relatedSentences: {
type: [String],
label: "Related Sentence",
optional: false,
blackbox: true,
defaultValue: []
},
现在我在控制台中得到以下结果。这些值现在作为带引号的数组返回,这是我所期望的。但是 $in 仍然没有将其视为一个数组。
["Ls6EyBkbcotcyfLyw", "6HKQroCjZhG9YCuBt"]
selector.js:595 Uncaught Error: $in needs an array
数据是如何填充的
回答@Kyll - 这就是最初填充数据的方式。我正在使用 AutoForm,
{{> afQuickField name='relatedSentences.0' value=this._id type="hidden"}}
然后通过钩子添加数组数据。
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 } );
});
}
});
【问题讨论】:
-
realated 应该看起来像
['Ls6EyBkbcotcyfLyw','6HKQroCjZhG9YCuBt']内部数组而不是字符串。错误告诉你同样的事情。$in只接受数组。 -
是的,我理解错误信息,但我不明白为什么 Meteor 将数组作为字符串返回
-
如何传递模板数据?
-
谢谢@Kyll 我不确定你的意思。模板是 showSentence,助手可以访问 this 和 relatedSentence 字段。
-
是的,但显然您的数据在途中以某种方式歪斜成字符串。那么它是如何进入您的模板数据的呢?通过路由器?车把呼叫..?
标签: meteor