I am learning how to use a backend so forgive me if this is elementary, it seems like I'm missing something simple. I am trying to create a simple CRUD app to learn so I chose to make a blog platform that I can incorporate into my website later.
I am using Angular, Node and Express, and PostgreSql.
I can successfully post entries into the database. I can see them on Postico when I click Enter. I just can't seem to figure out how to fire an alert that notifies if the entry was posted successfully or not. Right now I have the alert in the Angular service on a .success etc, but I have a feeling that's wrong. When I was searching for the answer here other people had it in the controller but I tried that and it didn't work.
Here's the relevant code I have:
createEntry.html
<form ng-submit="createBlogEntry(blog)">
<h2>Title:</h2>
<input type="title" ng-model="blog.title"></input>
<br>
<h3>Author:</h3>
<input type="author" ng-model="blog.author"></input>
<br>
<h3>Photo:</h3>
<input type="imageUrl" ng-model="blog.imageUrl"></input>
<br>
<h3>Content:</h3>
<textarea type="content" rows="5" cols="50" ng-model="blog.content">
</textarea>
<br>
<button type="submit">Save Entry</button>
</form>
createEntryCtrl.js The angular controller
angular.module("blog").controller("createEntryCtrl", function($scope, adminService) {
$scope.createBlogEntry = function(blog){
adminService.createBlogEntry(blog);
}
});
adminService.js The Angular service:
angular.module("blog").service("adminService", function($http) {
this.createBlogEntry = function(blog) {
$http.post('/api/createBlogEntry', blog)
.success(function(data) {
alert("Entry Posted");
})
.error(function(data) {
alert("Error in Posting");
})
}
});
index.js The Server Index (minus irrelevant endpoints)
// EXTERNAL MODULES //
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var massive = require('massive');
// CONFIG //
var config = require('./config');
// EXPRESS //
var app = module.exports = express();
app.use(express.static(__dirname + './../dist'));
app.use(bodyParser.json());
// MASSIVE //
var massiveUri = config.MASSIVE_URI;
var massiveServer = massive.connectSync({
connectionString: massiveUri
});
app.set('db', massiveServer);
var db = app.get('db');
var dbSetup = require('./services/dbSetup');
dbSetup.run();
// CONTROLLERS //
var userCtrl = require('./controllers/userCtrl');
var blogCtrl = require('./controllers/blogCtrl');
// Blog Endpoints //
app.post('/api/createBlogEntry', blogCtrl.createBlogEntry);
// CONNECTIONS //
var port = config.PORT;
app.listen(port, function() {
console.log('Listening on port ' + port);
});
blogCtrl.js Node Controller:
var app = require('./../index');
var db = app.get('db');
module.exports = {
createBlogEntry: function(req, res, next) {
console.log(res);
var blog = req.body;
db.blogs.blog_create([blog.title, blog.author, blog.imageUrl, blog.content], function(err, blog) {
if (err) {
return res.status(500).send(err);
}
})
}
};
blog_create.sql The Sql:
INSERT INTO blogs (title, author, imageUrl, content)
VALUES ($1, $2, $3, $4)
via Diego
No comments:
Post a Comment