Cannot get / Error in MEAN stack application - node.js

I'm following the tutorial of Heroku: https://devcenter.heroku.com/articles/mean-apps-restful-api
I followed every step without the deployment to Heroku server and worked in my localhost.
So when I open my application with the port localhost/8080 I found the error of (Cannot Get / ).
this is my server code :
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var CONTACTS_COLLECTION = "contacts";
var app = express();
app.use(bodyParser.json());
// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;
// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/test", { useNewUrlParser: true }, function (err, client) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = client.db();
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// CONTACTS API ROUTES BELOW
// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}
/* "/api/contacts"
* GET: finds all contacts
* POST: creates a new contact
*/
app.get("/api/contacts", function(req, res) {
db.collection(CONTACTS_COLLECTION).find({}).toArray(function(err, docs) {
if (err) {
handleError(res, err.message, "Failed to get contacts.");
} else {
res.status(200).json(docs);
}
});
});
app.post("/api/contacts", function(req, res) {
var newContact = req.body;
newContact.createDate = new Date();
if (!req.body.name) {
handleError(res, "Invalid user input", "Must provide a name.", 400);
} else {
db.collection(CONTACTS_COLLECTION).insertOne(newContact, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new contact.");
} else {
res.status(201).json(doc.ops[0]);
}
});
}
});
/* "/api/contacts/:id"
* GET: find contact by id
* PUT: update contact by id
* DELETE: deletes contact by id
*/
app.get("/api/contacts/:id", function(req, res) {
db.collection(CONTACTS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to get contact");
} else {
res.status(200).json(doc);
}
});
});
app.put("/api/contacts/:id", function(req, res) {
var updateDoc = req.body;
delete updateDoc._id;
db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to update contact");
} else {
updateDoc._id = req.params.id;
res.status(200).json(updateDoc);
}
});
});
app.delete("/api/contacts/:id", function(req, res) {
db.collection(CONTACTS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err, result) {
if (err) {
handleError(res, err.message, "Failed to delete contact");
} else {
res.status(200).json(req.params.id);
}
});
});
PS: when I run the command ng serve: the main page shows but no connection with the database it's just a static page .. please help
and I'm a beginner in this mean languages.

It seems that it misses a part in your server code.
You have configured your API part, which can be called by your Angular app, but you're not serving your Angular app.
You should listen to all GET request and send as only file your index.html which in fact should contain your Angular SPA.
Something like this:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
You can have a look at the Express doc: http://expressjs.com/fr/api.html#res.sendFile

Related

MEAN Stack App: Getting results from backend database query, but frontend is saying that it can't get any results

I have a MEAN stack app, most notably using Angular with a built in Express.JS backend. I've recently FUBAR'ed my local repo and rebuilt it from a clone of my remote repo, which doesn't include any of the changes I made to ruin my former repo and which is currently functionally deployed on Heroku. However, since I've cloned the repository, I've been having an issue where I am able to make a query directly to my backend with the "/api/monsters/" url and get the results from my database, but when the frontend attempts to do so it returns "Cannot GET /api/monsters".
Here is my Server.js file. This is the version that currently works in Heroku, but to get it working locally I've had to update distDir on line 12 to "var distDir = __dirname + "/dist/monster-playbook";". I also have to replace "process.env.MONGODB_URI" with the url of my database, which I can't post here or in my github repo because the url contains my login credentials.
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var MONSTER_COLLECTION = "monsters";
var app = express();
app.use(bodyParser.json());
// Create link to Angular build directory
var distDir = __dirname + "/dist";
app.use(express.static(distDir));
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;
// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI , function (err, client) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = client.db();
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 8080
, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}
app.get("/api/monsters", function(req, res) {
console.log('attempted');
db.collection(MONSTER_COLLECTION).find({}).toArray(function(err, docs) {
if (err) {
handleError(res, err.message, "Failed to get monsters.");
} else {
res.status(200).json(docs);
}
});
});
app.post("/api/monsters", function(req, res) {
var newMonster = req.body;
newMonster.createDate = new Date();
if (!req.body.name) {
handleError(res, "Invalid user input", "Must provide a name.", 400);
} else {
db.collection(MONSTER_COLLECTION).insertOne(newMonster, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new monster.");
} else {
res.status(201).json(doc.ops[0]);
}
});
}
});
app.get("/api/monsters/:id", function(req, res) {
db.collection(MONSTER_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to get monster");
} else {
res.status(200).json(doc);
}
});
});
app.put("/api/monsters/:id", function(req, res) {
var updateDoc = req.body;
delete updateDoc._id;
db.collection(MONSTER_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to update monster");
} else {
updateDoc._id = req.params.id;
res.status(200).json(updateDoc);
}
});
});
app.delete("/api/monsters/:id", function(req, res) {
db.collection(MONSTER_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err, result) {
if (err) {
handleError(res, err.message, "Failed to delete contact");
} else {
res.status(200).json(req.params.id);
}
});
});
Edit: Here's the code repo for this site: https://github.com/allredbm/Monster-Playbook.

How to get the sessionToken for currentUser?

I work on a web app hosted on heroku. As database I try to use back4app. With help of the back4app documention I have realized the log in. Since two days I try to get the session to work with it.
Therefore I have read a lot of blogs, articels and of course the parse documentation. But I'm not able to get it to work. I hope, you will find my problem. Following code is my lastest attempt:
const express = require('express');
const server = express();
const path = require('path');
const bodyParser = require('body-parser');
const port = process.env.PORT || 8080;
const back4app = require('parse/node');
back4app.initialize("xxx","yyy");
back4app.serverURL = 'https://parseapi.back4app.com/';
server.use(express.static('web'));
server.use(bodyParser.json());
server.get('/lgn', (req, resp) => {
console.log("server.get('/lgn',...");
resp.sendFile(path.join(__dirname + '/web/login.html'));
});
server.post('/lgn', (req, resp) => {
const data = req.body;
console.log("server.post('/lgn',...");
if(data.email != undefined){
console.log(data.email);
resetPassword(data);
} else{
logIn(data, function(err, user){
console.log(user.get("sessionToken"));
//How to get the user object in other routes?
console.log('session');
back4app.User.enableUnsafeCurrentUser(); //is this a secure solution?
back4app.User.currentAsync().then(function(userObj) {
console.dir(userObj.get("sessionToken"));
});
if(user){
resp.send( JSON.stringify( {url: '/'}) );
} else{
console.log(err);
}
});
}
});
function logIn(data, cb) {
// Create a new instance of the user class
var user = back4app.User
.logIn(data.username, data.password)
.then(function(user) {
console.log('User successful logged in with name: ' + user.get("username"));
cb(null, user);
})
.catch(function(error){
console.log("Error: " + error.code + " " + error.message);
cb(error);
});
}
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
The userObj is null. But why? What I have to do, that I get the currentUser and his session in other routes?
(I have also tryed to work with back4app.Session, but didn't get, what I want.)
It is unsafe to use the currentUser methods in a Node.js app.
Instead of:
logIn(data, function(err, user){
console.log(user.get("sessionToken"));
//How to get the user object in other routes?
console.log('session');
back4app.User.enableUnsafeCurrentUser(); //is this a secure solution?
back4app.User.currentAsync().then(function(userObj) {
console.dir(userObj.get("sessionToken"));
});
if(user){
resp.send( JSON.stringify( {url: '/'}) );
} else{
console.log(err);
}
});
Use:
logIn(data, function(err, user){
// This is your session token. You will have to send it back to your client. The client should store it and send it to the server in the other routes.
const sessionToken = user.get("sessionToken");
console.log(sessionToken);
//How to get the user object in other routes?
//In order to get the user in other routes you will have to use the sessionToken like this:
back4app.User.me({ sessionToken }).then(function(userObj) {
console.dir(userObj.get("sessionToken"));
});
if(user){
resp.send( JSON.stringify( {url: '/'}) );
} else{
console.log(err);
}
});

How to consume API which is created in nodejs-express-oracledb

I would want to consume GET/PUT/POST/DELETE API’s through a web page (HTML) which would be integrated on the Oracle JET.
The webpage will have one input free text option, where the user can type in to update the record.
i.e. If I click on one record on the JET dashboard, the API ‘GET /alluser/:FIRST_NAME – Read the profile of an user’ will be called,
it will display the detailed record with one text box as free text,
once the user enters text and hits submit, ‘PUT /process_post – Update the profile of an user’ API will be called, which will update the record.
Here is my script
var express = require('express');
var oracledb = require('oracledb');
var app = express();
var dbConfig = require('./dbconfig.js');
var bodyParser = require('body-parser');
var port = 3000;
app.use(bodyParser.json()); // Use body parser to parse JSON body
oracledb.outFormat = oracledb.OBJECT;
// Get a non-pooled connection
function run() {
oracledb.createPool({
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err) {
if (err)
console.error("createPool() error: " + err.message);
else
var server = app.listen(port,
function () {
console.log('Server is listening on port ' + server.address().port);
});
});
}
function doGetConnection(res, cb) {
oracledb.getConnection(function (err, connection) {
if (err) {
res.set('Content-Type', 'application/json');
res.status(500).send(JSON.stringify({
status: 500,
message: "Error getting DB connection",
detailed_message: err.message
}));
} else {
cb(err, connection);
console.log(" Connection is connected");
}
});
}
app.post('/process_post', function (req, res) {
console.log("contenttype"+req.get('Content-Type'))
doGetConnection(res, function(err, connection) {
if (err)
return;
connection.execute(
"INSERT INTO TEST_TABLE(FIRST_NAME,LAST_NAME) VALUES (:FIRST_NAME,:LAST_NAME)",
[(req.body.FIRST_NAME),(req.body.LAST_NAME) ],
{ autoCommit: true,
outFormat:oracledb.OBJECT
},
console.log("check2"),
function (err) {
console.log("check3");
if (err) {
console.log("check4");
res.set('Content-Type', 'application/json');
res.status(400).send(JSON.stringify({
status: 400,
message: "Input Error",
detailed_message: err.message
}));
} else {
// Successfully created the resource
res.status(201).set('Location', '/process_post/' + req.body.FIRST_NAME).end();
}
doRelease(connection, "POST /process_post");
});
});
});
app.get('/alluser', function (req, res) {
doGetConnection(res, function(err, connection) {
if (err)
return;
connection.execute(
"SELECT * from employees",
function (err, result) {
if (err) {
res.set('Content-Type', 'application/json');
res.status(500).send(JSON.stringify({
status: 500,
message: "Error getting the farmer's profile",
detailed_message: err.message
}));
} else {
res.contentType('application/json').status(200);
res.send(JSON.stringify(result.rows));
}
doRelease(connection, "GET /bananas");
});
});
});
app.get('/alluser/:FIRST_NAME', function (req, res) {
doGetConnection(res, function(err, connection) {
if (err)
return;
connection.execute(
"SELECT * from employees WHERE first_name = :f",
{ f: req.params.FIRST_NAME },
function (err, result) {
if (err) {
res.set('Content-Type', 'application/json');
res.status(500).send(JSON.stringify({
status: 500,
message: "Error getting the farmer's profile",
detailed_message: err.message
}));
} else if (result.rows.length < 1) {
res.set('Content-Type', 'application/json');
res.status(404).send(JSON.stringify({
status: 404,
message: "FIRST_NAME doesn't exist",
detailed_message: ""
}));
} else {
res.contentType('application/json');
res.status(200).send(JSON.stringify(result.rows));
}
doRelease(connection, "GET /user/" + req.params.FIRST_NAME);
});
});
});
function doRelease(connection, message) {
connection.close(
function(err) {
if (err)
console.error(err);
else
console.log(message + " : Connection released");
});
}
run();
Thank you.
Oracle JET can help you make API calls.
Here's an example from their Cookbook: http://www.oracle.com/webfolder/technetwork/jet/jetCookbook.html?component=crud&demo=model
Have a look at the documentation for Collection and Model.
It's a lot to read if you're getting into it for the first time, so I'll give a quick example for a GET all:
var dataModel = oj.Model.extend({urlRoot: 'example.com'});
var modelInstance = new dataModel();
var CollectionConfig = {
model: modelInstance,
url: 'example.com'
};
var dataCollection = oj.Collection.extend(CollectionConfig);
var collectionInstance = new dataCollection();
collectionInstance.fetch({
success: function (collection, response, options) {
console.log(response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});

how to replace process.env.GEOCODER_API_KEY in the routes

i export all the secret Api and password and cloudinary credentials to heroku using heroku config:set what syntax do i use now to replace process.env that i was using in development since i am not using .env file in the production. i am looking for syntax to replace the process.env
here is my code:
//----------------------------------------------------------------------------//
//--------------------------Dependencies For Route----------------------------//
//----------------------------------------------------------------------------//
var express = require("express");
var router = express.Router();
var Campground = require("../models/campground");
var middleware = require("../middleware");
var NodeGeocoder = require('node-geocoder');
var multer = require('multer');
var async = require("async");
//Node Geocoder API Configuration
var options = {
provider: 'google',
httpAdapter: 'https',
apiKey: process.env.GEOCODER_API_KEY,
formatter: null
};
var geocoder = NodeGeocoder(options);
//Multer Storage
var storage = multer.diskStorage({
filename: function(req, file, callback) {
callback(null, Date.now() + file.originalname);
}
});
//Multer Filter
var imageFilter = function (req, file, cb) {
// accept image files only
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/i)) {
return cb(new Error('Only image files are allowed!'), false);
}
cb(null, true);
};
//Storing Image + Filter
var upload = multer({ storage: storage, fileFilter: imageFilter});
//Cloudinary Configuration
var cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: 'dnposhqpc',
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
// INDEX - SHOW ALL CAMPGROUNDS
router .get("/", function(req, res){
var perPage = 8;
var pageQuery = parseInt(req.query.page);
var pageNumber = pageQuery ? pageQuery : 1;
var noMatch = null;
if (req.query.search) {
const regex = new RegExp(escapeRegex(req.query.search), 'gi');
// GET ALL CAMPGROUNDS FROM THE DB
Campground.find({"name": regex}, function(err, allCampgrounds){
if(err || !allCampgrounds){
console.log(err);
req.flash("error", "Something went wrong!");
} else {
if(allCampgrounds.length < 1){
noMatch = "No campground match that query, please try again.";
}
res.render("campgrounds/index", {campgrounds:allCampgrounds, page: 'campgrounds', noMatch: noMatch});
}
});
} else {
Campground.find({}).skip((perPage * pageNumber) - perPage).limit(perPage).exec( function(err, allCampgrounds){
Campground.count().exec(function (err, count) {
if(err) {
console.log(err);
} else {
res.render("campgrounds/index", {campgrounds:allCampgrounds, page: 'campgrounds', noMatch: noMatch,
campgrounds: allCampgrounds,
current: pageNumber,
pages: Math.ceil(count / perPage)
});
}
});
})
}
});
//----------------------------------------------------------------//
//-----------------CREATE NEW CAMPGROUNDS -----------//
//----------------------------------------------------------------//
//CREATE - ADD NEW CAMPGROUND TO DB
router.post("/", middleware.isLoggedIn, upload.single('image'), function(req, res){
// local variables
// Request The Name
var name = req.body.name;
// Request The Image
var image = req.body.image;
var imageId = req.body.imageId;
// Request The descriptions
var desc = req.body.descriptions;
// Request The Price
var price = req.body.price;
// Request The Author's ID + Username
var author = {
id: req.user._id,
username: req.user.username
};
//Location Code - Geocode Package
geocoder.geocode(req.body.location, function (err, data ) {
//Error Handling For Autocomplete API Requests
//Error handling provided by google docs -https://developers.google.com/places/web-service/autocomplete
if (err || data.status === 'ZERO_RESULTS') {
req.flash('error', 'Invalid address, try typing a new address');
return res.redirect('back');
}
//Error handling provided by google docs -https://developers.google.com/places/web-service/autocomplete
if (err || data.status === 'REQUEST_DENIED') {
req.flash('error', 'Something Is Wrong Your Request Was Denied');
return res.redirect('back');
}
// Error handling provided by google docs -https://developers.google.com/places/web-service/autocomplete
if (err || data.status === 'OVER_QUERY_LIMIT') {
req.flash('error', 'All Requests Used Up');
return res.redirect('back');
}
//Credit To Ian For Fixing The Geocode Problem - https://www.udemy.com/the-web-developer-bootcamp/learn/v4/questions/2788856
var lat = data[0].latitude;
var lng = data[0].longitude;
var location = data[0].formattedAddress;
//Reference: Zarko And Ian Helped Impliment the Image Upload - https://github.com/nax3t/image_upload_example
cloudinary.uploader.upload(req.file.path, function (result) {
//image variable needs to be here so the image can be stored and uploaded to cloudinary
image = result.secure_url;
imageId= result.public_id;
//Captures All Objects And Stores Them
var newCampground = {name: name, image: image, description: desc, price: price, author:author, location: location, lat: lat, lng: lng, imageId: imageId};
// Create a new campground and save to DB
Campground.create(newCampground, function(err, newlyCreated){
if(err || !newlyCreated){
//Logs Error
req.flash('error', err.message);
return res.redirect('back');
} else {
//redirect back to campgrounds page
//Logs Error
console.log(newlyCreated);
//Flash Message
req.flash("success", "Campground Added Successfully");
//Redirects Back To Featured Campgrounds Page
res.redirect("/campgrounds");
}
});
});
});
});
// NEW - SHOW FORM TO CREATE NEW CAMPGROUND
router.get("/new", middleware.isLoggedIn, function(req, res) {
res.render("campgrounds/new");
});
// SHOW - SHOW ONLY ONE CAMPGROUND FROM THE DB
router.get("/:id", function(req, res) {
// find campround with the Provided id
Campground.findById(req.params.id).populate("comment").exec(function(err, foundcampground){
if(err || !foundcampground){
console.log(err);
req.flash("error", "Sorry, that campground does not exist!");
res.redirect("back"); // you need to redirect the user in case there isn't anything found by the provided id
} else {
console.log(foundcampground);
// render the show template
res.render("campgrounds/show", {campground: foundcampground});
}
});
});
// EDIT CAMPGROUND ROUTE
router.get("/:id/edit", middleware.checkCampgroundOwnership, function(req, res) {
Campground.findById(req.params.id, function(err, foundcampground){
if(err || !foundcampground){
console.log(err);
req.flash("error", "campground not found");
return res.redirect("back");
}
res.render("campgrounds/edit", {campground: foundcampground});
});
});
//----------------------------------------------------------------//
//-----------------Update CAMPGROUNDS -----------//
//----------------------------------------------------------------//
// UPDATE CAMPGROUND ROUTE
router.put("/:id", middleware.checkCampgroundOwnership, upload.single('image'), function(req, res, next){
async.waterfall([
function(done) {
geocoder.geocode(req.body.campground.location, function (err, data) {
if (err || !data.length) {
req.flash('error', 'Invalid address');
return res.redirect('back');
}
done(null, data);
});
},
function(data, done) {
// handle image uploading
Campground.findById(req.params.id, function(err, foundCampground) {
if(err || !foundCampground) {
console.log(err);
req.flash("error", err.message);
return res.redirect("back");
} else {
done(null, foundCampground, data);
}
});
},
function(foundCampground, data, done) {
if(req.file) {
cloudinary.v2.uploader.destroy(foundCampground.imageId, function(err, result) {
if(err) {
req.flash("error", err.message);
return res.redirect("back");
} else {
done(null, foundCampground, data);
}
});
} else {
done(null, foundCampground, data);
}
},
function(foundCampground, data, done) {
// if new image uploaded, destroy the old one
if(req.file) {
cloudinary.uploader.upload(req.file.path, function(result) {
req.body.campground.imageId = result.public_id;
req.body.campground.image = result.secure_url;
done(null, foundCampground, data);
});
} else {
done(null, foundCampground, data);
}
},
function(foundCampground, data) {
// update
// var newCampground = {name: req.name, price: price, image: image, imageId: imageId, description: desc, author:author, location: location, lat: lat, lng: lng};
req.body.campground.lat = data[0].latitude;
req.body.campground.lng = data[0].longitude;
req.body.campground.location = data[0].formattedAddress;
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, campground){
if(err){
req.flash("error", err.message);
res.redirect("back");
} else {
req.flash("success","Successfully Updated!");
res.redirect("/campgrounds/" + campground._id);
}
});
}
], function(err) {
if (err) return next(err);
res.redirect('/campgrounds');
});
});
// DESTORY CAMPGROUND ROUTE
router.delete("/:id", middleware.checkCampgroundOwnership, function(req, res) {
Campground.findByIdAndRemove(req.params.id, function(err, foundcampground){
if(err || !foundcampground){
req.flash("error", "Something went wrong!");
res.redirect("/campgrouns");
} else {
req.flash("success", "You have successfully deleted a campground");
res.redirect("/campgrounds");
}
});
})
// Middleware
function escapeRegex(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
module.exports = router;
Thanks for your assistance
You use the same syntax.
For example, if I did:
heroku config:set EXAMPLE_NAME=sainteverest
then I can do the following:
console.log(process.env.EXAMPLE_NAME)
// sainteverest

Mongoose .save is not a function

JS:
var express = require('express');
var router = express.Router();
// Mongoose
var mongoose = require('mongoose');
var mongoosedb = 'DBURL';
mongoose.connect(mongoosedb);
var database = mongoose.connection;
database.on('error', console.error.bind(console, 'connection error:'));
database.once('open', function() {
console.log('Mongoose is connected.');
});
var taskSchema = mongoose.Schema({
name: String,
isDone: Boolean
});
var Tasks = mongoose.model('Tasks', taskSchema);
// Get all tasks
router.get('/tasks', function(req, res, next) {
Tasks.find(function(err, tasks) {
if(err) {
console.log('error');
res.send(err);
}
res.json(tasks);
});
});
// Get single task
router.get('/task/:id', function(req, res, next) {
Tasks.findOne({_id: mongojs.ObjectId(req.params.id)}, function(err, task) {
if(err) {
res.send(err);
}
res.json(task);
});
});
// Save task
router.post('/task', function(req, res, next) {
var task = req.body;
if(!task.title || !(task.isDone + '')) {
res.status(400);
res.json({
"error": "Bad Data"
});
} else {
task.save(function(err, task) {
if(err) {
res.send(err);
}
res.json(task);
})
}
});
// Delete task
router.delete('/task/:id', function(req, res, next) {
Tasks.remove({_id: mongojs.ObjectId(req.params.id)}, function(err, task) {
if(err) {
res.send(err);
}
res.json(task);
});
});
// Update task
router.put('/task/:id', function(req, res, next) {
var task = req.body;
var updTask = {};
if (task.isDone) {
updTask.isDone = task.isDone;
}
if (task.title) {
updTask.title = task.title;
}
if (!updTask) {
res.status(400);
res.json({
"error": "Bad Data"
});
} else {
task.update({_id: mongojs.ObjectId(req.params.id)}, updTask, {}, function(err, task) {
if(err) {
res.send(err);
}
res.json(task);
});
}
});
module.exports = router;
I'm simply just trying to add a record to MongoDB using Mongoose.
After some Googling I couldn't find answer. My .find and .findOne methods work fine but for some reason I get the error task.save is not a function. What am I doing wrong?
I think your task creation is being done incorrectly.
First go ahead and import your model up at the top with your dependencies so we can access the model methods directly:
var express = require('express');
var router = express.Router();
// Mongoose
var mongoose = require('mongoose');
var mongoosedb = 'DBURL';
var Task = require('mongoose').model('Task'); // add your Task model
Then, in your save a task code, change it to this:
// Save task
router.post('/task', function(req, res, next) {
var task = new Task(req.body) // See http://mongoosejs.com homepage demo example for basic creation
if(!task.title || !(task.isDone + '')) {
res.status(400);
res.json({
"error": "Bad Data"
});
} else {
task.save(function(err, task) {
if(err) {
res.send(err);
}
res.json(task);
})
}
});
As your current Task creation is setup, you're just setting it to the req.body contents but not actually initializing an instance via the mongoose methods.
I personally like to use mongoose promises instead, just because I tend to like the style more. But you could also do something like this, if you have given access to your Task model via grabbing it as a dependency (as shown above):
Task.create(req.body)
.then(function(newTask) {
console.log("Task created successfully.");
return res.json(newTask); // using res.json here
})
.catch(function(err) {
console.log("There was a problem creating task.");
return res.status(500).json(err); // sends status with json obj
})
Anyhow, I hope this helps, let me know if this works, if not I'll check back later and see!
Your variable task is the body object you have defined previously :
var task = req.body;
If you want to generate a Tasks document from it, use :
var newTask = new Tasks(task);
newTask.save(function(err, task) {
if (err) {
res.send(err);
}
res.json(task);
});
Check mongoose Models documentation
You have also confused task.update with Tasks.update

Resources