如何与sails.js / waterline进行交易?

我试图把我的查询进入事务,我在运行时失败。 我得到的错误是:

Object #<bound> has no method 'transaction' 

我试图按照这个 “文档”。

总之,我的模型看起来像这样:

 updateOrCreate: function (profile_id, positive,negative) { var deferred = Q.defer(); Reputation.transaction().findOne().where({profile: profile_id}).then(function (rep) { if (rep) { // Reputation logic rep.save(function (err) {deferred.resolve();}); } else { // Reputation does not exist. Create. Reputation.create({profile: profile_id, positive: positive,negative:negative}).exec(function (e, rep) { deferred.resolve();}); } }).fail(function (err) {deferred.reject()}); return deferred.promise; } 

任何想法我做错了什么?

谢谢。

W上。

您所遵循的“文档”是一个关于如何将交易支持添加到Sails的build议。 Sails中没有本地事务支持。 有关如何使用MySQL或Postgres适配器的.query方法执行事务的示例,请参阅此答案 。

现在在sails v1中支持这个function(2017年6月26日还没有正式发布)。 您可以在next.sailsjs.com中查看此链接以获取文档: Datastore.transaction()

在上面的doc中是以下示例:

 sails.getDatastore() .transaction(function (db, proceed) { BankAccount.findOne({ owner: req.session.userId }).usingConnection(db) .exec(function (err, myAccount) { if (err) { return proceed(err); } if (!myAccount) { return proceed(new Error('Consistency violation: Database is corrupted-- logged in user record has gone missing')); } BankAccount.findOne({ owner: req.param('recipientId') }).usingConnection(db) .exec(function (err, recipientAccount) { if (err) { return proceed(err); } if (!recipientAccount) { err = new Error('There is no recipient with that id'); err.code = 'E_NO_SUCH_RECIPIENT'; return proceed(err); } // Do the math to subtract from the logged-in user's account balance, // and add to the recipient's bank account balance. var myNewBalance = myAccount.balance - req.param('amount'); // If this would put the logged-in user's account balance below zero, // then abort. (The transaction will be rolled back automatically.) if (myNewBalance < 0) { err = new Error('Insufficient funds'); err.code = 'E_INSUFFICIENT_FUNDS'; return proceed(err); } // Update the current user's bank account BankAccount.update({ owner: req.session.userId }) .set({ balance: myNewBalance }) .usingConnection(db) .exec(function (err) { if (err) { return proceed(err); } // Update the recipient's bank account BankAccount.update({ owner: req.param('recipientId') }) .set({ balance: recipientAccount.balance + req.param('amount') }) .usingConnection(db) .exec(function (err) { if (err) { return proceed(err); } return proceed(); }); }); }); }); }).exec(function(err){ // At this point, we know that, if our code above passed through // an error to `proceed`, Sails took care of rolling back the // transaction. Otherwise, it committed it to the database. if (err && err.code === 'E_INSUFFICIENT_FUNDS') { return res.badRequest(err); } else if (err && err.code === 'E_NO_SUCH_RECIPIENT') { return res.notFound(); } else if (err) { return res.serverError(err); } // All done! return res.ok(); }); 

似乎他们不支持这个 。 你可以使用像这样的东西: