What is wrong with my method?


#1

Hello, I want get a specífic search, but I can’t.
I think that the use publish all collection will be downloaded in client (25 mbs) is no good idea, or not?
The problem is that CodigosPostales.find({codigo:'16090'}); don´t work, this is the message in console.log


Next I will put my files, hardcoded

My File: imports/api/codigosPostales/methods.js

import { Meteor }           from 'meteor/meteor';
import { ValidatedMethod }  from 'meteor/mdg:validated-method';
import { DDPRateLimiter }   from 'meteor/ddp-rate-limiter';
import { _ }                from 'meteor/underscore';
import { CodigosPostales }  from './collection.js';

export const obtenerColonias = new ValidatedMethod({
  name: 'codigosPostales.obtenerColonias',
  validate: new SimpleSchema({
    cp: { type: String }
  })
  .validator(),
  run({cp}) {
    console.log(cp);
    // HARDCORING document: {codigo:'16090'}
    var resultado = CodigosPostales.find({codigo:'16090'});
    console.log(resultado);
  }
});

const CODIGOS_POSTALES_METODOS = _.pluck([obtenerColonias], 'name');
if (Meteor.isServer) {
  DDPRateLimiter.addRule({
    name(name) {
      return _.contains(CODIGOS_POSTALES_METODOS, name);
    },
    connectionId() {
      return true;
    },
  }, 5, 1000);
}

My File: imports/api/codigosPostales/index.js

export * from './collection';

My File: imports/api/codigosPostales/collections.js

import { Mongo } from 'meteor/mongo';
export const CodigosPostales = new Mongo.Collection('codigosPostales');

CodigosPostales.deny({
    insert() {return true;},
    update() {return true;},
    remove() {return true;}
});

imports/startup/server/register-api.js

import '../../api/codigosPostales/index.js';
import '../../api/codigosPostales/methods.js';

My File: server/main.js

import '/imports/startup/server';

Please, helpme
Thanks for read and for your time


#2

Try: CodigosPostales.find({codigo:'16090'}).fetch()

You shouldn’t publish 25MB worth of data to the client.
You should build a UI which manages the subscription to the server - selectively subscribe to data.


#3

Hey there!
its works!

Thanks for yor time!


#4

no worries!

if you run: Collection.find() it will return a cursor… you need to run .fetch() to get the array.
if you are only expecting a single result, you can use Collection.findOne() - this will return the array directly, no need to run .fetch() on this.


#5

I will remember this!

Thank you very much for you time, I really am very gratefull