2016-11-14 50 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

相关问题