简体   繁体   中英

Feathers service does not found during fetch request

I make post request, by the fetch, to add new user to database. The response that i get is 'POST http://localhost:3000/register 404 (Not Found)'. The problem is in the fact that, even after returning 404 to frontend, backend still continue the operation and adds user to DB.

Fetch request on frontend:


let res = await fetch('http://localhost:3000/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                login: login,
                password: password,
                role: role
            })
        });

Backend feathers service:


const registrator = {
    async create(data, params) {
        let Model = app.get('Model');
        Model.create({ login: data.login, password: data.password }, (err) =>{
            if (err) {
                return err;
            }
            else {
                Right.create({ login: data.login, rights: data.role },(err)=>{
                    if (err) {
                        Model.findOneAndRemove({ login: data.login });
                        return err;
                    }
                    else {
                        return data;
                    }
                });
            }
        });
    }
}

String that defines path to this service:


app.use('/register', registrator);

You are using callbacks which do not wait in an async function unless you turn them into a JavaScript Promise eg using the NodeJS built in utils.promfisy .

Most current NodeJS libraries support returning Promises already which will make things much easier to read and follow and also ensure that errors are handled properly (in your example it won't do anything in either case).

const registrator = {
    async create(data, params) {
        const Model = app.get('Model');
        const login = await Model.create({ login: data.login, password: data.password });
        const right = await Right.create({ login: data.login, rights: data.role });

        // check for errors and call this if necessary
        // await Model.findOneAndRemove({ login: data.login });

        return right; // login?  what does your client expect?
    }
}

To fix this, you should return something from main body of service. If 'return' is inside promise, node defines it as service that do not return anything, that's why we get this error. My service after fixing:

const registrator = {
    async create(data, params) {
        let Model = app.get('Model');
        return await Model.create({ login: data.login, password: data.password })
            .then(() => {
                Right.create({ login: data.login, rights: data.role })
            })
            .then(() => {
                return data;
            }).catch(err => {
                Model.findOneAndRemove({ login: data.login });
                Sentry.captureException(err);
            });
    }
}

This way 'data' would return only if operation was successful, so we can check result of operation on frontend part.

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