簡體   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