Reputation: 77
Hi im getting Parsing error: Unexpected token with no unexpected token if that makes sense. normally it tells you which token is unexpected but not for me.
In my code it says on line x there is an unexpected ) but when i delete that ) the error just moves to the bottom of the page where there is no code and just give the same error but without mentioning what the unexpected token is.
I have looked over google for an answer but its probably somone ; or ) i have missed somewhere so i related to my code but i cant seem to find it.
Here is the code where i get the first unexpected token )
app.put("/index/new_weight", function(req,res){
Exe.findById(req.body.exercise_id, function(err, array){
if (err){
console.log(err);
res.send(err);
}else{
var value = [0];
var oldArray = array.previous;
var newArray = req.body.exe.previous;
for (var i = 0; i < newArray.length; i++){
if (newArray[i] === ""){
value.push(oldArray[i]);
}else{
value.push(newArray[i]);
}
Exe.findByIdAndUpdate(req.body.exercise_id, {$set : {"previous" : value}}, function(err, newArray){
if (err){
console.log(err);
}else{
res.redirect("/index");
}
});
}
};
}); <<<< unexpected token ) apparently...
If i remove said ) the error just moves to the bottom of the page. Same error but doesnt specify what the unexpected token is....
Here is my full code in case the error is coming from above or below. I realize im probably just missing a ) or } somewhere but i cant find.
// REQUIRE REQUIRED FRAMEWORKS AND LIBS
var express = require("express");
var mongoose = require("mongoose");
var bParser = require("body-parser");
var ejs = require("ejs");
var mOverride = require("method-override");
//INITIALIZE EXPRESS, SETUP DB AND CALL LIBS
var app = express();
mongoose.connect("mongodb://localhost/exerciseApp");
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bParser.urlencoded({extended: true}));
app.use(mOverride("_method"));
//DB TEMPLATE
var exerciseSchema = new mongoose.Schema({
user: String,
exercise: [String],
previous: [Number],
latest: [Number]
});
var Exe = mongoose.model("Exe", exerciseSchema);
//**********************TESTING PURPOSES******************************
//CREATE TEST DATA
// Exe.create({
// user: "Template",
// exercise: ["Test"],
// previous: [0],
// latest: [0]
// });
// Exe.create({
// user: "Nathan",
// exercise: ["Bench Press"],
// previous: [35],
// latest: []
// });
// Exe.findOne({name: "Marc"}, function(err, test){
// console.log(test);
// });
//****************************************************
//ROUTES
app.get("/", function(req,res){
res.redirect("/index");
});
app.get("/index", function(req,res){
Exe.find({}, function(err, exercise){
if (err){
res.render("Oops, Somthing went wrong!");
}else{
res.render("index", {exes: exercise});
}
});
});
app.get("/index/new", function(req,res){
res.render("new");
});
app.put("/index", function(req,res){
Exe.update({}, {$push: req.body.exe}, {multi: true}, function(err, numAffected){ //ADDS NEW WORKOUT TO EACH USER
if (err){
console.log(err);
}else{
Exe.update({}, {$push: {"previous" : 0}}, {multi: true}, function(err, numAffected){ //PUSH STARTING VALUE OF 0 INTO PREVIOUS WEIGHT.
if (err){
console.log(err);
}else{
res.redirect("/index");
}
});
}
});
});
app.put("/index/new_weight", function(req,res){
Exe.findById(req.body.exercise_id, function(err, array){
if (err){
console.log(err);
res.send(err);
}else{
var value = [0];
var oldArray = array.previous;
var newArray = req.body.exe.previous;
for (var i = 0; i < newArray.length; i++){
if (newArray[i] === ""){
value.push(oldArray[i]);
}else{
value.push(newArray[i]);
}
Exe.findByIdAndUpdate(req.body.exercise_id, {$set : {"previous" : value}}, function(err, newArray){
if (err){
console.log(err);
}else{
res.redirect("/index");
}
});
}
});
});
app.get("/index/new/user", function(req,res){
res.render("newuser");
});
app.post("/index/new/user", function(req,res){
Exe.findOne({},{},{sort: {create_at: -1}}).lean().exec(function (err, templateSchema){ //.LEAN AND EXEC RETURNS COLLECTION MODEL AS A JS OBJECT.
if (err){
console.log(err);
res.send(err);
} else{
delete templateSchema["_id"];
templateSchema.user = req.body.exe.user;
Exe.create(templateSchema);
res.redirect("/index");
}
});
});
// SETUP SERVER AND LISTEN ON PORT.
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Excercise App Running...");
});
Thanks in advance for any answers!
Upvotes: 0
Views: 1015
Reputation: 571
Here is your code fixed, you miss a }
for the for
loop and a )
for the Exe.findById
function
app.put("/index/new_weight", function (req, res) {
Exe.findById(req.body.exercise_id, function (err, array) {
if (err) {
console.log(err);
res.send(err);
} else {
var value = [0];
var oldArray = array.previous;
var newArray = req.body.exe.previous;
for (var i = 0; i < newArray.length; i++) {
if (newArray[i] === "") {
value.push(oldArray[i]);
} else {
value.push(newArray[i]);
}
Exe.findByIdAndUpdate(req.body.exercise_id, { $set: { "previous": value } }, function (err, newArray) {
if (err) {
console.log(err);
} else {
res.redirect("/index");
}
});
}
}
});
});
Upvotes: 2