When i run this code with nodejs, it returns me this error: "SyntaxError: Unexpected Identifier" in this line...
var user_schema = New Schema({
^^^^^^^
i do not know what is the problem... (sorry for my english, i speak spanish)
---------------- APP.JS CODE ------------------
var http = require("http");
var express = require("express");
var app = express();
var jade = require("jade");
var mongodb = require("mongodb");
var mongoose = require('mongoose');
var user = require("./public/js/users").user;
var bodyparser = require("body-parser");
app.set('view engine', 'jade');
mongoose.connect("mongodb://localhost/SoR");
app.use(express.static("public"));
app.use("/public",express.static("public"));
app.use(bodyparser.json());
//app.use(bodyparser.urlEncoded({extended: true}));
app.get("/login",function(req,res){
res.render("login");
});
app.get("/",function(req,res){
res.render("index");
});
app.get("/signup",function(req,res){
res.render("signup");
});
app.post("/users",function(req,res){
var user = new user({email: req.body.email, username: req.body.username, password: req.body.password});
user.save(function(){
console.log(req.body.email);
console.log(req.body.password);
console.log(req.body.id);
res.send("save succesfull");
});
});
app.listen(8080);
---------------USERS.JS CODE -----------
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var user_schema = New Schema({
email: String,
username: String,
password: String
});
var user = mongoose.model("user","user_schema");
mongoose.connect("mongodb://localhost/SoR");
module.exports.user = user;
Change the New
to new
. new
is the Keyword to create a object using the constructor function in javascript.
Javascript doesn't recognize the New
operator that you used in the code and hence you get the error "SyntaxError: Unexpected Identifier" as New
is an unexpected identifier.
From MDN
A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
var user_schema = New Schema({
email: String,
username: String,
password: String
});
to
var user_schema = new Schema({
email: String,
username: String,
password: String
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.