这可能与我今天早上更新Meteor有关。
无论如何,这里有一些代码:
//this returns Words properly
console.log('splitter', splitSentenceIntoWordsForward(sentence))
var wordsForward = splitSentenceIntoWordsForward(sentence)
//if I remove this line then everything works.
UserSentences.insert({user: Meteor.userId(), words: wordsForward})
//If I don't remove the line above, this prints an array of empty objects like this: [ { }, { } ]
console.log("wordsForward new", wordsForward, JSON.stringify(wordsForward))
看来insert正在以某种方式破坏数组中的对象。
我的原始代码已经工作了好几周。只有今天早上才停止。 (除了meteor更新之外还有其他的东西可以参与。我添加了simpleschema和collection2。我也改变了一些发布方法,但是这个代码完全在服务器上运行。
这是更多代码:
UserSentences = new Mongo.Collection('userSentences')
Schemas.UserSentences = new SimpleSchema({
words: {
type: [Object],
label: 'Words',
optional: false
},
user: {
type: String,
label: 'user id',
optional: false
}
})
UserSentences.attachSchema(Schemas.UserSentences)
Meteor.methods({
createArticle: function (text, title, share) {
var article = {}
var wordsArray = []
article.title = title
article.userID = Meteor.userId()
article.text = text
article.public = share
var id = Articles.insert(article)
article.paragraphs = []
var paragraphs = splitArticleIntoParagraphs(text)
console.log("paragraphs", paragraphs)
paragraphs = paragraphs.filter(String)
console.log('paragraphs w/o empties', paragraphs)
_.each(paragraphs, function (paragraph, p) {
// console.log("paragraph", paragraph)
article.paragraphs[p] = {}
article.paragraphs[p].read = false
article.paragraphs[p].text = paragraph
article.paragraphs[p].index = p
// console.log("paragraphs[p]", article.paragraphs[p])
var sentences = splitParagraphIntoSentences(paragraph)
article.paragraphs[p].sentences = []
_.each(sentences, function (sentence, s) {
if (sentence !== "") {
article.paragraphs[p].sentences[s] = {}
// console.log("sentence", sentence)
article.paragraphs[p].sentences[s].text = sentence
article.paragraphs[p].sentences[s].read = false
// console.log("paragraphs[p].sentences[s]", article.paragraphs[p].sentences[s])
//console.log('splitter', splitSentenceIntoWordsForward(sentence))
var wordsForward = splitSentenceIntoWordsForward(sentence)
UserSentences.insert({user: Meteor.userId(), words: JSON.parse(JSON.stringify(wordsForward))})
// console.log("wordsForward new", wordsForward, JSON.stringify(wordsForward))
article.paragraphs[p].sentences[s].forward = {}
article.paragraphs[p].sentences[s].forward.words = wordsForward
// console.log('wordsForward', JSON.stringify(wordsForward))
// var wordsReverse = splitSentenceIntoWordsReverse(sentence)
_.each(wordsForward, function (word, w) {
if (word) {
wordsArray.push(word._id)
// console.log('this word should split', word)
_.each(word.simplified.split(""), function (character) {
console.log(character)
wordsArray.push(Words.findOne({simplified: character})._id)
})
article.paragraphs[p].sentences[s].forward.words[w] = {}
article.paragraphs[p].sentences[s].forward.words[w].wordID = word._id
// article.paragraphs[p].sentences[s].forward.words[w].simplified = word.simplified
// article.paragraphs[p].sentences[s].forward.words[w].trad = word.trad
// console.log("word.simplified", word.simplified)
var characters = word.simplified.split('')
// console.log("characters", characters)
article.paragraphs[p].sentences[s].forward.words[w].characters = []
/**/
_.each(characters, function (character, c) {
if (character) {
// console.log("character", character, p, s, w, c)
article.paragraphs[p].sentences[s].forward.words[w].characters[c] = {}
article.paragraphs[p].sentences[s].forward.words[w].characters[c].text = character
article.paragraphs[p].sentences[s].forward.words[w].characters[c].wordID = Words.findOne({simplified: character})._id
}
})
}
})
}
})
})
console.log("done")
article.words = wordsArray
article.finishedCreating = true
Articles.update({_id: id}, {
$set: {
paragraphs: article.paragraphs,
words: wordsArray,
finishedCreating: true
}
}, {bypassCollection2: true})
return id
}
})
var splitSentenceIntoWordsForward = function (string) { // DictionaryForwardMaximumMatchingOld
// console.log(new Date().toDateString())
console.log('typeof', typeof string)
var foundWordsArray = []
var position = string.length
while (position > 0) {
// console.log("while")
var index = position
var partialString = string.slice(0, index)
// console.log("partial sub: " + partialString.substring(0, 20))
var found = Words.findOne({
"simplified": partialString.substring(0, 20)
})
// console.log(partialString)
if (found) {
// console.log("found: " + found.simplified)
// if (found.pinyin == "" && foundWordsArray.length > 1) { //punctuation at beginning of sentence isn't a wrapping concern.
// console.log("found punctuation")
// foundWordsArray[foundWordsArray.length - 1].simplified = foundWordsArray[foundWordsArray.length - 1].simplified + found.simplified
// } else {
foundWordsArray.push(found)
console.log("found", found.simplified)
// }
position = position - 1
var partialStringCount = partialString.length
while (partialStringCount > 0) {
// console.log("second while")
string = string.substring(1)
partialStringCount -= 1
}
position = string.length
index = position
}
else if (partialString.length == 1) {
var newEntryID = Words.insert({
trad: partialString,
simplified: partialString,
pinyin: ""
})
foundWordsArray.push(Words.findOne({
newEntryID
}))
var partialStringCount = partialString.length
while (partialStringCount > 0) {
string = string.slice(1, string.length) //remove first character
partialStringCount -= 1
}
position = string.length
index = position //advance(string.startIndex, position)
console.log("added " + partialString)
}
else {
// console.log("else")
position = position - 1
index = position
}
}
//console.log("found: ", JSON.stringify(foundWordsArray))
// console.log(new Date().toDateString())
return foundWordsArray
}
答案 0 :(得分:0)
Collection2导致了这个问题。
这就是我解决这个问题的方法:
UserSentences.insert({user: Meteor.userId(), words: wordsForward},
{bypassCollection2: true})