【问题标题】:Processing array of objects from Machine in treeline.io在 treeline.io 中处理来自 Machine 的对象数组
【发布时间】:2015-04-02 00:27:12
【问题描述】:

我在 treeline.io 中创建了一个自定义机器,它搜索 twitter 并返回我想作为模型插入到我的数据库中的对象数组。

这里是返回数组的格式:

[
  {
    "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": ""
  }
]

我正在尝试使用“每个”机器并告诉它将数组中的每个项目传递给我的“创建帖子”机器,该机器应该创建一个新帖子。但是,当我尝试执行此操作时,它会引发大量错误,我猜它在理解给出的对象时遇到问题,或者某些值不匹配。不管怎样,我有点卡住了。

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

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.' } ] }

【问题讨论】:

    标签: json node.js sails.js treeline


    【解决方案1】:

    循环机器是我们在 Treeline 中迭代的东西(不是双关语)。您遇到的问题是因为 Treeline 为您生成的 Create Post 机器每个模型属性都有一个输入,但 Each 机器天真地只向“工人”发送一个输入。

    为此,我们将推出几个解决方案,但与此同时,最简单的方法是创建您自己的 Create Post from Dictionary 机器,这是一个接受单个输入的电路 (例如dict),它与 ​​Post 对象的模式相匹配,并包含 Create Post 的单个实例,其中每个输入都由 dict 的属性填充。然后您可以使用Create Post from Dictionary 作为Each 的“工人”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多