Reputation: 23
Below is my code in the postController.js
, with which I am trying to save user created posts to the MongoDB:
const postsCollection = require('../db').db().collection("posts")
let Post = function(data) {
this.data = data
this.errors = []
}
Post.prototype.cleanUp = function() {
if (typeof(this.data.title) != "string") {
this.data.title = ""
} {
if (typeof(this.data.body) != "string") {
this.data.body = ""
} {
// get rid of silly properties
this.data = {
data: this.data.title.trim(),
body: this.body.title.trim(),
createdDate: new Date()
}
}
Post.prototype.validate = function() {
if (this.data.title == "") {
this.errors.push("Please provide a title.")
}
if (this.data.body == "") {
this.errors.push("Please provide post input.")
}
}
Post.prototype.create = function() {
return new Promise((resolve, reject) => {
this.cleanUp()
this.validate()
if (!this.errors.length) {
// save post in the database
postsCollection.insertOne(this.data).then(() => {
resolve()
}).catch(() => {
this.errors.push("Please try again later.")
reject(this.errors)
})
} else {
reject(this.errors)
}
})
}
module.exports = Post
However, I am unable to see or locate the error, as it is showing the following error in the Terminal which is line one in the code above:
SyntaxError: Unexpected end of input
at Object. (C:#######*******\controllers\postController.js:1:14)
Upvotes: 1
Views: 133
Reputation: 23
@simpleDmitry: Sorry, I was trying to make it bold; noticed after the post was gone.
@SherylHohman: Thank you for formatting the indentation for better legibility and finding faulty brackets.
@sunday & Ali Rehman: Thank you for pointing the one too many braces in Post.prototype.cleanUp function which I have corrected and now reads as:
Post.prototype.cleanUp = function() {
if (typeof(this.data.title) != "string") {this.data.title = ""}
if (typeof(this.data.body) != "string") {this.data.body = ""}
// get rid of silly properties
this.data = {
title: this.data.title.trim(),
body: this.body.title.trim(),
createdDate: new Date()
}
}
The page is now pointing to an empty page showing only { }. I have to further dig into why.. Have a nice time to all.
Upvotes: 0
Reputation: 2993
I think the error is on Post.prototype.cleanUp
function. You have 2 opening keys {
at the end of each if
inside this function.
Upvotes: 1