0

I tried a lot of things on my tests, but in the end I came back to the original code from angular-fullstack, and then I tried to clone the working example. Anyway, I cannot get it working: My changes don't synchronize between clients, as the example ones does.
This controller is the simplest one, and I was trying to use it as an example of how to work with socket.io with the rest of the app.

My Controller (simplified)

'use strict';

(function() {

class AdminController {
  constructor($http, $scope, socket) {
    this.$http = $http;
    this.info = [];
    $http.get('/api/infoclan').then(response => {
      this.info.push(response.data);
      socket.syncUpdates('infoclan', this.info);
    });
  }

  save() {
      this.$http.post('/api/infoclan/update', this.info[0]);
  }
}

angular.module('eecrApp.admin')
  .controller('AdminController', AdminController);
})();

I'm using push because it was giving me the array.push error, as the response is a JSON, and in the socket.service.js the service try to push the 'new item':

<line 41>
if (oldItem) {
            array.splice(index, 1, item);
            event = 'updated';
          } else {
            array.push(item);
          }

With my scope as an array, it save, everything works fine...except the change don't synchronize between clients (I need to refresh).
I tried different approaches...with the same result.

Index.js on Server

'use strict';

import {Router} from 'express';
import * as controller from './infoclan.controller';
import * as auth from '../../auth/auth.service';

var router = new Router();

router.get('/', controller.index);
router.post('/update', auth.hasRole('admin'), controller.update);
router.post('/', controller.create);

export default router;

Infoclan.model.js8

'use strict';

var mongoose = require('bluebird').promisifyAll(require('mongoose'));
import {Schema} from 'mongoose';

var InfoclanSchema = new mongoose.Schema({
  identificador: { //It'll always save the same document, as there's only one infoclan
    type: String,
    default: '1'
  },
  nombre: {
    type: String,
    default: 'Elite EspaƱa'
  },
  twitter: {
    type: String,
    default: 'EliteEspanaCR'
  },
  texto: {
    type: String,
    default: 'Cambiar!test'
  }
});

export default mongoose.model('Infoclan', InfoclanSchema);

I don't know what I should try next :(

Ark
  • 13
  • 5

1 Answers1

1

Ok, I found it :) :+1:

The problem were o the API, with the GET Index. I was doing this:

export function index(req, res) {
  Infoclan.findOneAsync({'identificador': '1'}, '-_id nombre twitter texto')
    .then(handleEntityNotFound(res))
    .then(respondWithResult(res))
    .catch(handleError(res));
}

Without the _id, it couldn't identify the oldItem, and then it never updates, only creates.
I need to send the _id of every object, then :)
This is the correct code:

export function index(req, res) {
  Infoclan.findOneAsync({'identificador': '1'})
    .then(handleEntityNotFound(res))
    .then(respondWithResult(res))
    .catch(handleError(res));
}
Ark
  • 13
  • 5