Wednesday 19 December 2018

Struggling with ASYNC / AWAIT and Mongoose

Hello all,​I'm trying to use ASYNC / AWAIT with Mongoose, and I'm missing something:​I have a file that holds the functions for talking to MongoDB:​const mongoose = require("mongoose");mongoose.connect("mongodb+srv://niodbuser:We.like.M0NG0@nasquam-z7win.mongodb.net/exampleApp?retryWrites=true",{ useNewUrlParser: true }).then(() => console.log("Connection to the remote database made...")).catch(err => {console.error("Could not connect to MongoDB...", err.stack);process.exit(1);});const userInfoSchema = new mongoose.Schema({firstName: String,lastName: String,email: String,password: String,date: { type: Date, default: Date.now },isPublic: Boolean});const Users = mongoose.model("user", userInfoSchema);async function getAllUsers(){const users = await Users.find();let result = users;console.log(result);return result;}module.exports.getAllUsers = getAllUsers;​The routes file imports my user-auth fileconst user = require("../controllers/user-auth");const routes = function(app) {app.get("/api-v1/users", (req, res) => {let results = user.getAllUsers();res.send(results);});}module.exports.routes = routes;​When I do my get request with Postman the users print in the console, however nothing is being returned to my main file. An empty JSON object is being sent back to the browser.​What am I missing here?Kind regards​

Submitted December 19, 2018 at 06:08PM by sma92878

No comments:

Post a Comment