Skip to content

Transaction is not working #144

@alickmail

Description

@alickmail

I am not able to use transaction for model update, the transaction.commit().then() is fired, but the data on google console did not updated.

I have tried the Model.update function without using transaction, the data on google console is updated successfully

i am following the example from gitbook:
Link

Here is update model code:

function transferCoins(fromUser, toUser, coins) {
  return new Promise((resolve, reject) => {
    let fromUserObj = fromUser.plain();
    let toUserObj = toUser.plain();

    let transaction = gstore.transaction();
    transaction.run().then(() => {
      User.update(fromUserObj.id, {
        name : 'asdsadfssssf',
        coins: fromUserObj.coins - 1000
      }, null, null, transaction);

      transaction.commit().then((x) => {
        resolve();
      }).catch(err => {
        console.log({err});
      });

      // User.update(toUserObj.id, toUserObj.coins + 1000, null, null, transaction);
    }).catch(err => {
      transaction.rollback();
      reject(err);
    });
  });
}

Here is the model file

const gstore = require('gstore-node')();
const Schema = gstore.Schema;
const Joi = require('joi');

const userSchema = new Schema({
  name        : {joi: Joi.string().required()},
  lastname    : {joi: Joi.string()},
  password    : {joi: Joi.string()},
  coins       : {joi: Joi.number().integer().min(0)},
  email       : {joi: Joi.string().email()},
  createdAt   : {joi: Joi.date()},
  access_token: {joi: [Joi.string(), Joi.number()]},
  birthyear   : {joi: Joi.number().integer().min(1900).max(2013)},
}, {joi: true});


const User = gstore.model('User', userSchema);

module.exports = User;

Please help, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions