简体   繁体   中英

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);

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 aka optimistic ui aka 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM