dry-scarletD
Refine3y ago
15 replies
dry-scarlet

Receive data from database

How can I receive data from mongoDB with refine?

user.routes.js:
router.route('/invite').post(sendInvitation).get(getAllInvitations);;

user.controller.js:
const getAllInvitations = async (req, res) => {
const { _end, _order, _start, _sort, email_like = "" } = req.query;

const query = {};

if (email_like) {
query.email = { $regex: email_like, $options: "i" };
}

try {
const count = await Invitation.countDocuments(query);

const invitations = await Invitation
.find(query)
.populate("parentUserID")
.limit(parseInt(_end, 10))
.skip(parseInt(_start, 10))
.sort({[_sort]: _order === "ASC" ? 1 : -1});

res.header("x-total-count", count);
res.header("Access-Control-Expose-Headers", "x-total-count");

res.status(200).json(invitations);
} catch (error) {
res.status(500).json({ message: error.message });
}
}

invitation schema:
import mongoose from 'mongoose';

const invitationSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId, // Auto-generated by MongoDB
parentUserID: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
name: { type: String, required: true },
surname: { type: String, required: true },
email: { type: String, required: true },
token: { type: String, required: true },
});

export default mongoose.model('Invitation', invitationSchema);


Whatever I'm trying on front-end it is not working, the request keeps loading
Was this page helpful?