mgons
mgons

Reputation: 343

Meteor method 404 error but logs in console

I call Meteor method but it is showing error message:

Method 'users.insertotp' not found [404]

But somehow the console.log get executed, why?

what am i doing wrong here?

You can see the code here github.

File: imports/api/db.js

export const Otp   = new Mongo.Collection('otp');
Meteor.methods({
    'users.insertotp' (otp) {
        // if(!this.userId) {
        //     throw new Meteor.Error('not-authorized');
        // }
        console.log('otp',otp);
        return Otp.insert({
            otp,
            userId: this.userId,
            updatedAt: moment().valueOf()
        });
    }
});

File: imports/ui/Signup.js

Meteor.call('users.insertotp',1234);

Upvotes: 1

Views: 87

Answers (1)

ghybs
ghybs

Reputation: 53350

Your imports/api/db.js file is imported on the Client but not on the Server.

Your client/main.js defines some routes, among which imports/ui/Signup.js which does import the imports/api/db.js and calls the Method right away.

On the other hand, your server/main.js imports only imports/api/users.js, which does not import other api file.

Therefore your Client knows the Method, hence it is able to print the log in the browser console, thanks to Meteor latency compensation a.k.a. optimistic ui a.k.a. client stub. But your Server does not know it, hence it responds with a 404 error.

Simply also import the db file in your server main entry file, and Meteor will do its magic.

Upvotes: 1

Related Questions