2016-11-14 45 views
0

我有一個Meteor方法執行API調用,然後將調用的響應保存到用戶集合中。我在我的項目中使用了Collection2軟件包,爲此,我有點迷失了設置我的SimpleSchema將API調用結果保存到集合中,但在設置SimpleSchema時收到錯誤

這裏是JSON響應的樣子從API調用:

[{ 「關鍵詞」: 「2I」, 「URL」: 「http://example.com」, 「標題」 :「Example」,「timestamp」:「2016-11-05 08:54:42」,「ip」:「00.00.00.000」,「clicks」:「2」,「user」:「HweoSCY2ujscjJ9Zl」}, {「keyword」:「2j」,「url」:「http://example.com」,「title」:「YouTube」,「timestamp」:「2016-11-06 02:11:18」,「 IP 「:」 00.00.00.000" , 「點擊次數」: 「1」, 「用戶」: 「HweoSCY2ujscjJ9Zl」},{ 「關鍵字」: 「2K」, 「URL」: 「http://example.com」,」 title「:」YouTube「,」timestamp「:」2016-11-08 03:35:12「,」ip「:」00.00.00.000「,」clicks「:」0「,」user「:」HweoSCY2ujscjJ9Zl「 }]

這裏是目前我是如何能夠將這些數據保存到用戶的集合:

Meteor.users.update(Meteor.userId(), { 
    $set: { 'shortURLs.URLS': result.data } 
}); 

這工作,看起來像這樣的DB:

screenshot

我的問題是我希望爲此設置一個SimpleSchema設置,以便將「時間戳」保存爲日期而不是字符串,但是每次嘗試爲其創建模式時,我都會收到像"After filtering out keys not in the schema, your modifier is now empty"這樣的錯誤。我已經嘗試了很多不同的變化,試圖使它的工作,但他們都沒有成功,這裏只是目前那裏的AT:

Schema.ShortURLs = new SimpleSchema({ 
    shortURLs: { 
     type: Object 
    }, 
    'shortURLs.$': { 
     type: Object 
    }, 
    'shortURLs.$.keyword': { 
     type: String, 
     optional: true, 
     label: "Keyword" 
    }, 
    'shortURLs.$.url': { 
     type: String, 
     optional: true, 
     label: "URL" 
    }, 
    'shortURLs.$.title': { 
     type: String, 
     optional: true, 
     label: "Title" 
    }, 
    'shortURLs.$.timestamp': { 
     type: Date, 
     optional: true, 
     label: "Timestamp" 
    }, 
    'shortURLs.$.ip': { 
     type: String, 
     optional: true, 
     label: "IP" 
    }, 
    'shortURLs.$.clicks': { 
     type: String, 
     optional: true, 
     label: "Clicks" 
    }, 
    'shortURLs.$.user': { 
     type: String, 
     optional: true, 
     label: "User" 
    }, 
}); 

這則連接用戶簡單的模式相距:

... 
shortURLs: { 
    type: Schema.ShortURLs, 
    optional: true 
}, 
... 

而且我有一個連接到用戶的集合:

Meteor.users.attachSchema(Schema.User); 

我不認爲有一個與我有它重視如何,因爲我有其他SimpleSchemas建立以同樣的方式與第一個問題眼睛工作正常,我相信問題是我如何寫這個特定的。任何幫助在這裏將不勝感激。

回答

1

您需要Meteor.users集合中定義shortURLs類型type: [Schema.ShortURLs],即類型的列表Schema.ShortURLs

Schema = {} 
Schema.ShortURLs = new SimpleSchema({ 
    keyword: { 
     type: String, 
     optional: true, 
     label: "Keyword" 
    }, 
    url: { 
     type: String, 
     optional: true, 
     label: "URL" 
    }, 
    title: { 
     type: String, 
     optional: true, 
     label: "Title" 
    }, 
    timestamp: { 
     type: Date, 
     optional: true, 
     label: "Timestamp" 
    }, 
    ip: { 
     type: String, 
     optional: true, 
     label: "IP" 
    }, 
    clicks: { 
     type: String, 
     optional: true, 
     label: "Clicks" 
    }, 
    user: { 
     type: String, 
     optional: true, 
     label: "User" 
    }, 
}); 

Schema.User = new SimpleSchema({ 
... 
    shortURLs: { 
     type: [Schema.ShortURLs], 
     optional: true 
    }, 
... 
}); 

... 

Meteor.users.attachSchema(Schema.User); 
+0

非常感謝您的回覆,我設法得到它與這個工作。我只是有一個問題,每當我現在調用這個方法,我看到這樣的消息出現在我的控制檯爲每個數組:'「SimpleSchema.clean:autoconverted值2016-11-11 23:44:28從字符串到對象的shortURLs 。$。時間戳「'。你知道更好的方式去做這件事嗎?在某些情況下,API結果返回100+,因此多次看到這個彈出窗口讓我覺得我可能沒有正確/最有效地做某件事,但也許我錯了。任何意見都非常感謝,再次感謝! :) – U54

+0

這是正常的,因爲從api返回的日期值的原始類型是一個字符串。您可以通過設置'SimpleSchema.debug = false'來停止此操作,但也可能會漏掉SimpleSchema包中的其他警告。 – JeremyK

相關問題