在 treeline.io 中处理来自机器的对象数组

Processing array of objects from Machine in treeline.io

我在 treeline.io 中创建了一个自定义机器,用于搜索 Twitter 和 returns 我想作为模型插入到我的数据库中的对象数组。

返回数组的格式如下:

[
  {
    "postText": "RT @TheJWittz: MEWTWOOOOOOOOO http://t.co/ZzZsiGIDE4",
    "postID": "583388726883180500",
    "postStatus": "new",
    "postDate": "Wed Apr 01 22:01:25 +0000 2015",
    "postScheduleDate": "",
    "postUserImageURL": "http://pbs.twimg.com/profile_images/569178372899487744/YqHkhlli_normal.jpeg",
    "postUserRealName": "",
    "postUserName": "",
    "postUpdateUser": "",
    "postType": "twitter",
    "postStatusDate": "2015-04-01T22:01:50.745Z",
    "postHasImage": true,
    "postImagePreviewURL": "http://pbs.twimg.com/media/CBiczM2UoAAMSQa.png:small",
    "postImageURL": "http://pbs.twimg.com/media/CBiczM2UoAAMSQa.png",
    "postHasVideo": false,
    "postVideoPreviewURL": "",
    "postVideoURL": ""
  }
]

我正在尝试使用 "each" 机器并告诉它将数组中的每个项目传递到我的 "Create Post" 机器,它应该创建一个新的 Post。然而,当我尝试这样做时它会抛出大量错误,我猜它在理解它所给的对象时遇到了问题,或者某些值不匹配。不管怎样,我有点卡住了。

这是我在加载路由时在控制台中得到的内容:

error: Sending 500 ("Server Error") response: 
 Error: `create_post` machine encountered 17 error(s) while validating runtime input values.
    at /Users/garethjeanne/Documents/HTL2/htl-treeline/node_modules/machine/lib/Machine.prototype.exec.js:53:19
    at Machine_prototype_exec [as exec] (/Users/garethjeanne/Documents/HTL2/htl-treeline/node_modules/machine/lib/Machine.prototype.exec.js:61:9)
    at /Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/machines/map.js:79:29
    at /Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/async/lib/async.js:249:17
    at /Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/async/lib/async.js:125:13
    at Array.forEach (native)
    at _each (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/async/lib/async.js:46:24)
    at async.each (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/async/lib/async.js:124:9)
    at _asyncMap (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/async/lib/async.js:248:13)
    at /Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/async/lib/async.js:219:23
    at Object.module.exports.fn (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/machines/map.js:76:5)
    at _runMachine (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/machine/lib/Machine.prototype.exec.js:281:17)
    at afterCacheLookup (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/machine/lib/Machine.prototype.exec.js:288:7)
    at _cacheLookup (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/machine/lib/Machine.prototype.exec.js:160:25)
    at Machine_prototype_exec [as exec] (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/machines/69aac477-d97c-4280-a225-39a345fdbd3f_0.3.0/node_modules/machinepack-loops/node_modules/machine/lib/Machine.prototype.exec.js:211:5)
    at Object.sails.machines.c1191290-9d8f-4ce4-a84e-74621c88a99d_0.0.1.get_minus_search_minus_term.setEnvironment.exec.success (/Users/garethjeanne/Documents/HTL2/htl-treeline/api/controllers/FetchController.js:35:28) { [Error: `create_post` machine encountered 17 error(s) while validating runtime input values.]
  code: 'E_MACHINE_RUNTIME_VALIDATION',
  machine: 'create_post',
  reason: '`create_post` machine encountered 17 error(s) while validating runtime input values.',
  status: 400,
  errors: 
   [ { [Error: 2 error(s) validating value:
      [ { [Error: An invalid value was specified: 
       { postText: '@RussyViC get the badger hawk',
         postID: '583424534688243700',
         postStatus: 'new',
         postDate: 'Thu Apr 02 00:23:43 +0000 2015',
         postScheduleDate: '',
         postUserImageURL: 'http://pbs.twimg.com/profile_images/574797837021675521/lBWqP0uV_normal.jpeg',
         postUserRealName: '',
         postUserName: '',
         postUpdateUser: '',
         postType: 'twitter',
         postStatusDate: '2015-04-02T00:24:11.826Z',
         postHasImage: false,
         postImagePreviewURL: '',
         postImageURL: '',
         postHasVideo: false,
         postVideoPreviewURL: '',
         postVideoURL: '' }

       This doesn't match the specified type: 
       'string'] code: 'E_INVALID_TYPE' },
       { [Error: An invalid value was specified: 
       { postText: '@RussyViC get the badger hawk',
         postID: '583424534688243700',
         postStatus: 'new',
         postDate: 'Thu Apr 02 00:23:43 +0000 2015',
         postScheduleDate: '',
         postUserImageURL: 'http://pbs.twimg.com/profile_images/574797837021675521/lBWqP0uV_normal.jpeg',
         postUserRealName: '',
         postUserName: '',
         postUpdateUser: '',
         postType: 'twitter',
         postStatusDate: '2015-04-02T00:24:11.826Z',
         postHasImage: false,
         postImagePreviewURL: '',
         postImageURL: '',
         postHasVideo: false,
         postVideoPreviewURL: '',
         postVideoURL: '' }

       This cannot be coerced into the specified type: 
       'string'] code: 'E_COERCION' } ]] code: 'E_INVALID_TYPE', minor: false, errors: [Object] },
     { [Error: `postID` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postID',
       reason: '`postID` is a required input- but it was not defined.' },
     { [Error: `postStatus` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postStatus',
       reason: '`postStatus` is a required input- but it was not defined.' },
     { [Error: `postDate` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postDate',
       reason: '`postDate` is a required input- but it was not defined.' },
     { [Error: `postUserImageURL` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postUserImageURL',
       reason: '`postUserImageURL` is a required input- but it was not defined.' },
     { [Error: `PostUserName` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'PostUserName',
       reason: '`PostUserName` is a required input- but it was not defined.' },
     { [Error: `PostUserRealName` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'PostUserRealName',
       reason: '`PostUserRealName` is a required input- but it was not defined.' },
     { [Error: `postType` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postType',
       reason: '`postType` is a required input- but it was not defined.' },
     { [Error: `postStatusDate` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postStatusDate',
       reason: '`postStatusDate` is a required input- but it was not defined.' },
     { [Error: `postHasImage` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postHasImage',
       reason: '`postHasImage` is a required input- but it was not defined.' },
     { [Error: `postImagePreviewURL` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postImagePreviewURL',
       reason: '`postImagePreviewURL` is a required input- but it was not defined.' },
     { [Error: `postImageURL` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postImageURL',
       reason: '`postImageURL` is a required input- but it was not defined.' },
     { [Error: `postScheduleDate` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postScheduleDate',
       reason: '`postScheduleDate` is a required input- but it was not defined.' },
     { [Error: `postUpdateUser` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postUpdateUser',
       reason: '`postUpdateUser` is a required input- but it was not defined.' },
     { [Error: `postHasVideo` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postHasVideo',
       reason: '`postHasVideo` is a required input- but it was not defined.' },
     { [Error: `postVideoPreviewURL` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postVideoPreviewURL',
       reason: '`postVideoPreviewURL` is a required input- but it was not defined.' },
     { [Error: `postVideoURL` is a required input- but it was not defined.]
       code: 'E_INPUT_REQUIRED',
       input: 'postVideoURL',
       reason: '`postVideoURL` is a required input- but it was not defined.' } ] }

循环机是我们在 Treeline 中迭代(没有双关语意)的东西。您遇到的问题是由于 Treeline 为您生成的 Create Post 机器每个模型属性有一个输入,但 Each 机器天真地只发送一个输入到 "worker".

为此,我们想到了几个即将推出的解决方案,但与此同时,最简单的做法是创建您自己的 Create Post from Dictionary 机器,这是一个接受电路的电路与 Post 对象的架构匹配的单个输入(例如 dict),并包含 Create Post 的单个实例,其中每个输入都由 [=13= 的属性填充].然后你可以使用 Create Post from Dictionary 作为 Each 的 "worker"。