简体   繁体   English

模式猫鼬意外标识符

[英]schema mongoose unexpected identifier

When i run this code with nodejs, it returns me this error: "SyntaxError: Unexpected Identifier" in this line... 当我使用nodejs运行此代码时,它在此行中向我返回此错误: “ SyntaxError:意外的标识符” ...

var user_schema = New Schema({ var user_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更改为new new is the Keyword to create a object using the constructor function in javascript. new是使用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. Javascript无法识别您在代码中使用的New运算符,因此您会收到错误“ SyntaxError:意外的标识符”,因为New是意外的标识符。

From MDN 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. 当JavaScript引擎在解析代码时遇到不符合该语言语法的令牌或令牌顺序时,将引发SyntaxError。

var user_schema = New Schema({
    email: String,
    username: String,
    password: String
});

to

var user_schema = new Schema({
    email: String,
    username: String,
    password: String
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM