繁体   English   中英

我在我的节点 js 代码中收到错误意外的输入结束

[英]i am getting error unexpected end of input in my node js code

 const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
  const mongoose = require('mongoose');

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({
 extended: true
}));
app.use(express.static("public"));


mongoose.set('useUnifiedTopology', true);

mongoose.connect("mongodb://localhost:27017/restDB",{useNewUrlParser:true});

const articleSchema={
 title:String,
content:String
 }
 const Article = mongoose.model("Article",articleSchema);

app.get("/articles",function(req,res){
  Article.find(function(err,foundArticles){
  if(!err){
   res.send(foundArticles)
  }else{
   res.send(err)
   }

  });
 });

   app.post("/articles",function(req,res){
     const newArticle = Article({
    title:  req.body.title,
     content:  req.body.content
    });
   newArticle.save(function(err){
    if(!err){
    res.send("succes")
     }else{
    res.send(err)
    }
       })
   app.delete("/articles",function(req,res){
       Article.deleteMany(function(err){
      if(!err){
       res.send("success")
    }else{
     res.send(err)
    }
   });
    });



    app.listen(3000, function() {
      console.log("Server started on port 3000");
     });

我正在通过邮递员发出请求,同样的代码运行良好,但在此错误代码运行良好之前它开始出现此错误,我不明白发生了什么

    SyntaxError: Unexpected end of input
      at wrapSafe (internal/modules/cjs/loader.js:1071:16)
     at Module._compile (internal/modules/cjs/loader.js:1121:27)

        at internal/main/run_main_module.js:18:47

请帮助我,它不包含 html 文件,我正在使用邮递员请求并使用 mongodb 作为数据库

请重新检查您的右括号

您带有端点文章的 api 未关闭。

 app.post("/articles", function (req, res) {
    const newArticle = Article({
        title: req.body.title,
        content: req.body.content
    });
    newArticle.save(function (err) {
        if (!err) {
            res.send("succes")
        } else {
            res.send(err)
        }
    });
});

SyntaxError: Unexpected end of input error通常意味着您在代码中的某处缺少右括号、大括号、方括号或类似字符,或者您正在尝试JSON.parse()缺少此类字符的字符串。

通过 javascript linter(例如 jshint)运行您的代码/JSON 字符串以查找语法错误。

暂无
暂无

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

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