with-mongo-connection icon indicating copy to clipboard operation
with-mongo-connection copied to clipboard

Look like it not work anymore :c

Open IRediTOTO opened this issue 4 years ago • 1 comments

Thank you for made this example but look like it not right way. :( I got this error when run
ReferenceError: Cannot access 'mongoose' before initialization

import mongoMiddleware from "../../../lib/api/mongo-middleware";
import apiHandler from "../../../lib/api/api-handler";

export default mongoMiddleware(async (req, res, connection, models) => {
    const {
        query: { name },
        method,
    } = req;

    apiHandler(res, method, {
        POST: (response) => {
            // I just try to get a user
            models.Users.findById("5e9877725f7aa94364d4bc46", (error, user) => {
                if (error) {
                    connection.close();
                    response.status(500).json({ error });
                } else {
                    console.log(user);
                    response.status(200).json(user);
                    connection.close();
                }
            });
        },
    });
});

IRediTOTO avatar Apr 25 '20 02:04 IRediTOTO

The error massage is quite obvious, at the time this lines are getting execute there is no real connection to database, so can't find an specific record by it's id. Instead you should parameterize the callback to accept an id to search for and expose it as an endpoint to be able to call it from front end.

niiima avatar Jan 07 '21 22:01 niiima