将 API 调用结果保存到集合中,但在设置 SimpleSchema 时收到错误
Saving API call result to a collection but recieving errors while setting up a SimpleSchema
我有一个执行 API 调用的 Meteor 方法,然后将调用的响应保存到用户集合中。为此,我正在使用 Collection2 package with my project and I'm a little bit lost setting up my SimpleSchema。
下面是 API 调用的 JSON 响应:
[{"keyword":"2i","url":"http://example.com","title":"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","clicks":"1","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"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 }
});
这在数据库中有效并且看起来像这样:
我的问题是我想为此设置一个 SimpleSchema,这样 "timestamp" 将被保存为日期而不是字符串,但每次我尝试为它创建一个模式时我只收到像 "After filtering out keys not in the schema, your modifier is now empty"
这样的错误。我已经尝试了很多不同的变体来尝试让它发挥作用,但是 none 其中已经成功,这里只是目前的位置:
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,并且它们工作正常,我相信问题在于我如何编写这个特定的。如有任何帮助,我们将不胜感激。
您需要将 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);
我有一个执行 API 调用的 Meteor 方法,然后将调用的响应保存到用户集合中。为此,我正在使用 Collection2 package with my project and I'm a little bit lost setting up my SimpleSchema。
下面是 API 调用的 JSON 响应:
[{"keyword":"2i","url":"http://example.com","title":"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","clicks":"1","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"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 }
});
这在数据库中有效并且看起来像这样:
我的问题是我想为此设置一个 SimpleSchema,这样 "timestamp" 将被保存为日期而不是字符串,但每次我尝试为它创建一个模式时我只收到像 "After filtering out keys not in the schema, your modifier is now empty"
这样的错误。我已经尝试了很多不同的变体来尝试让它发挥作用,但是 none 其中已经成功,这里只是目前的位置:
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,并且它们工作正常,我相信问题在于我如何编写这个特定的。如有任何帮助,我们将不胜感激。
您需要将 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);