Function defined as Method not found


#1

Am building a very simple app based on what I’ve leaned by going through the ‘social’ tutorial. Having found that adding or updating collection records from the client is ‘unsafe’ I’m moving that operation to a Method - but when I try to call the method function from client code, the compiler says it can’t find the named function.

My method is in /collections/methods.ts

import { loadWordlewords } from '../server/load-wordlewords.ts';
import { Meteor } from 'meteor/meteor'
import { check } from 'meteor/check';
import { WordleWords } from './wordlewords.ts';

Meteor.methods({
  addWord2: function(newWord:string) {
    check(newWord, String);

    console.log("This is the addWord2 method");
    /**
    var look = WordleWords.findOne({word: newWord });

    if (look) { 
    
      WordleWords.update({_id: look._id}, {$inc: {number: 1}});
    
    } else {

      WordleWords.insert({word: newWord, number: 1});
    }
    */
  }
});

My app.ts and main.ts files both include
import '../collections/methods.ts';

My client attempts to call the function thus:
<form [ngFormModel]="wordleForm" #f="ngForm" (submit)="addWord2(f.value)">

If I try to call the Method indirectly via the client .ts file I still get the same warning from the compiler:
Cannot find name 'addWord2'

…even if the client .ts file also contains:
import '../../collections/methods.ts';

The browser doesn’t give a 404 and the page renders correctly - but the form doesn’t do anything. Ideas?


#2

Guess it should be Meteor.call("addWord2", f.value)
And also! Take care about perfomance!

Meteor.methods({
  addWord2: function(newWord:string) {
    check(newWord, String);
    WordleWords.upsert({word: newWord}, {$set: {word: newWord}, $inc: {number: 1}});
  }
});

#3

If you are not seeing an error in the console or in the terminal(server) then that means u did not import the method file to the server and only the client knows about the method.


#4

Meteror.call works - thanks, but…

WordleWords.upsert({word: newWord}, {$set: {word: newWord}, $inc: {number: 1}});

…doesn’t work - it neither inserts a new record nor updates an existing one.


#5

I have exactly the same problem, everything inside the meteor method will be undefined and out of scope. I have had to resort to not having the feature in my app now because was no way to work around it.


#6

It depends where your method is defined. Make sure method is on server-side and WordleWords is available (imported) into module.

You may debug your method or just trace it

Meteor.methods({
  addWord2: function(newWord:string) {
    check(newWord, String);
    return WordleWords.upsert({word: newWord}, {$set: {word: newWord}, $inc: {number: 1}});
  }
});

Then on the client side

Meteor.call('addWord2', function(err, res) {
  console.log(res); // reports about affected changes
});

#7

Adding “return” at the start of the line actually makes it worse - I then get:

=> Errors prevented startup:

   While processing files with barbatus:typescript (for target web.browser):
   collections/methods.ts:19:2: ',' expected.

   While processing files with barbatus:typescript (for target os.windows.x86_32):
   collections/methods.ts:19:2: ',' expected.

=> Your application has errors. Waiting for file change.

#8

Do you have a repo we can look at? @mrzafod is giving good advice, but it’s hard to help without a full picture of the code.


#9

Sorry, no. Where would I put it?


#10

I was thinking github.


#11

I don’t have a paid subscription and don’t want to make it a public repository.


#12

How about a minimal reproduction?


#15

Nah - forget it - can’t get the hang of github.


#16

guess this path is showing that methods are not on the server?


#17

I just did what the socially tutorial does - methods.ts file in /collections and import that into /server/main.ts using:

import '../collections/methods.ts';

Also in /server/main.ts is:
import {loadWordlewords} from './load-wordlewords.ts';


#18

What I just discovered I hadn’t done was to include the methods.ts file on both server and client - unless it was also included on the client side, only the view of the client that made the change was updated.


#19

Getting used to Meteor since it’s restructured and requires imports everywhere has been a nightmare, and this thread was super helpful. Thanks all.