Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试将帖子保存到MongoDB时出现意外的输入结束错误_Javascript_Mongodb_Controller - Fatal编程技术网

Javascript 尝试将帖子保存到MongoDB时出现意外的输入结束错误

Javascript 尝试将帖子保存到MongoDB时出现意外的输入结束错误,javascript,mongodb,controller,Javascript,Mongodb,Controller,下面是我在postController.js中的代码,我正试图用它将用户创建的帖子保存到MongoDB: const postsCollection = require('../db').db().collection("posts") let Post = function(data) { this.data = data this.errors = [] } Post.prototype.cleanUp = function() { if (typeof(this.dat

下面是我在
postController.js
中的代码,我正试图用它将用户创建的帖子保存到MongoDB:

const postsCollection = require('../db').db().collection("posts")

let Post = function(data) {
  this.data = data
  this.errors = []
}

Post.prototype.cleanUp = function() {
    if (typeof(this.data.title) != "string") {
      this.data.title = ""
    } {
      if (typeof(this.data.body) != "string") {
        this.data.body = ""
      } {


        // get rid of silly properties
        this.data = {
          data: this.data.title.trim(),
          body: this.body.title.trim(),
          createdDate: new Date()
        }
      }

      Post.prototype.validate = function() {
        if (this.data.title == "") {
          this.errors.push("Please provide a title.")
        }
        if (this.data.body == "") {
          this.errors.push("Please provide post input.")
        }
      }

      Post.prototype.create = function() {
        return new Promise((resolve, reject) => {
          this.cleanUp()
          this.validate()
          if (!this.errors.length) {
            // save post in the database
            postsCollection.insertOne(this.data).then(() => {
              resolve()
            }).catch(() => {
              this.errors.push("Please try again later.")
              reject(this.errors)
            })
          } else {
            reject(this.errors)
          }
        })
      }

      module.exports = Post
但是,我无法看到或定位错误,因为它在终端中显示以下错误,即上面代码中的第一行:

SyntaxError:输入意外结束
反对。(C:######*********\controllers\postController.js:1:14)


您在
module.exports=Post
之前缺少一个
}
,我认为错误在
Post.prototype.cleanUp
函数上。您在每个函数的末尾都有两个打开键
{
,如果
在该函数中。

@simpleDmitry:对不起,我试图将其加粗;在帖子消失后注意到了

@谢丽尔·霍曼:谢谢你格式化缩进以提高易读性并找到错误的括号

@sunday&Ali Rehman:感谢您在Post.prototype.cleanUp函数中指出了一个太多的括号,我已经更正了它,现在的内容如下:

Post.prototype.cleanUp = function() {
 if (typeof(this.data.title) != "string") {this.data.title = ""}
 if (typeof(this.data.body) != "string") {this.data.body = ""}

  // get rid of silly properties
  this.data = {
  title: this.data.title.trim(),
  body: this.body.title.trim(),
  createdDate: new Date()
 }
}
该页面现在指向一个空页面,仅显示 { }. 我必须进一步探究原因。。
祝大家玩得愉快。

为什么要使用**….**?请在导出文件之前尝试添加},如果删除了错误请告诉我。根据您发布的代码,我修复了您的缩进。在每个嵌套处使用2或4个空格。你有1或2或0-非常不一致-这使得阅读代码很困难。正确/一致的缩进也有助于找到缺少的括号。