in terminal, run node
then paste in these lines:
require("dotenv").config();
// Connect to mongodb:
require("./config/database");
// Load up our models:
const User = require("./models/user");
const Score = require("./models/score");
Then you can do this to get all users:
User.find({}, (e, users) => { console.log(users) })
To set up your own node console (npm run console
):
make a console.js
:
require("dotenv").config();
// Lets us create our own node console/repl and prompt for JS code:
var repl = require("repl");
// Start REPL and provide custom prompt :)
var replServer = repl.start({
prompt: "Node Console> ",
});
// Connect to mongodb:
require("./config/database");
// Load up our models:
const User = require("./models/user");
const Score = require("./models/score");
replServer.context.User = User;
replServer.context.Score = Score;
console.log("\n");
console.log("========================================");
console.log(
"Console loaded. To get all users type: User.find({}, (e, users) => { console.log(users) })"
);
console.log("========================================");
add
"console": "node console.js"
to package.json scripts object.
So it should look like this:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "npm run build",
"console": "node console.js"
},
Now to run it, just type npm run console
and you'll have a node REPL with your database loaded 😎