风帆 Model.create

Sails Model.create

我正在按照一本书自学 Sails。

书中的代码应该是:

Video.create(foundVideos).exec(function(err, videoRecordsCreated){
            if(err){
              console.log('err', err);

              return cb(err);
            }
            console.log('videoRecordsCreated', videoRecordsCreated);
          });

然而 运行 sails lift 我收到以下错误:

err { UsageError: Invalid new record. Details: Got an array, but expected new record to be provided as a dictionary (plain JavaScript object). Array usage is no longer supported as of Sails v1.0 / Waterline 0.13. Instead, please explicitly call .createEach().

at Object.success (/Users/bliss/Documents/Coder/Sails/brushfire/config/bootstrap.js:72:17)
at afterwards (/Users/bliss/Documents/Coder/Sails/brushfire/node_modules/machinepack-youtube/node_modules/machine/lib/intercept-exit-callbacks.js:131:21)
at Timeout._onTimeout (/Users/bliss/Documents/Coder/Sails/brushfire/node_modules/machinepack-youtube/node_modules/machine/lib/intercept-exit-callbacks.js:98:20)
at ontimeout (timers.js:488:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:283:5)   name: 'UsageError',   code: 'E_INVALID_NEW_RECORD',   details: 'Got an array, but expected

new record to be provided as a dictionary (plain JavaScript object). Array usage is no longer supported as of Sails v1.0 / Waterline 0.13. Instead, please explicitly call .createEach().' }

如何解决这个问题? 看起来代码应该在以前版本的 .create 中工作,但现在不再支持数组使用....

如何重写此代码以使用最新的 Sails 版本?

Waterline 的 create 方法要求参数为对象。

根据您遇到的错误,foundVideos 似乎是一个对象数组。在这种情况下,您需要使用需要对象数组的 createEach 。像这样:

  Video.createEach(foundVideos).fetch().exec(function(err, videoRecordsCreated){
    if(err){
      console.log('err', err);

      return cb(err);
    }
    console.log('videoRecordsCreated', videoRecordsCreated);
  }));