繁体   English   中英

javascript编译有问题

[英]Having issues with javascript compiling

我有一个应用程序 Javascript 文件,它在 VS Studio Code 的控制台部分显示错误,它一直说:app.js:解析器希望找到一个 '}' 来匹配这里的 '{' 标记。 并且控制台将无法编译。

根据我的代码,您能告诉我需要在大括号中关闭的位置吗? 我想我可能会感到困惑。

这是我的 app.js 代码:

const express = require('express');
const app = express();
const bodyParser  = require('body-parser');
const mongoose = require('mongoose');
//specify where to find the schema
const Item = require('./models/item')
// connect and display the status 
mongoose.connect('mongodb://localhost:27017/items', { useNewUrlParser: true })
  .then(() => { console.log("connected"); })
  .catch(() => { console.log("error connecting"); });

// use the following code on any request that matches the specified mount path
app.use((req, res, next) => {
   console.log('This line is always called');
   res.setHeader('Access-Control-Allow-Origin', '*'); //can connect from any host
   res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS'); //allowable methods
   res.setHeader('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept');
   next();
});
app.get('/items', (req, res, next) => {
  //call mongoose method find (MongoDB db.Students.find())
  Item.find() 
    //if data is returned, send data as a response 
    .then(data => res.status(200).json(data))
    //if error, send internal server error
    .catch(err => {
    console.log('Error: ${err}');
    res.status(500).json(err);
});


  // parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// serve incoming post requests to /items
app.post('/items', (req, res, next) => {
    const items = req.body;
    console.log(items.itemName + " " + items.servings);
    //sent an acknowledgment back to caller 
    res.status(201).json('Post successful');
  });


//to use this middleware in other parts of the application
module.exports=app;

您的app.get('/items'功能未关闭。试试这个。

 app.get('/items', (req, res, next) => { //call mongoose method find (MongoDB db.Students.find()) Item.find() //if data is returned, send data as a response .then(data => res.status(200).json(data)) //if error, send internal server error .catch(err => { console.log('Error: ${err}'); res.status(500).json(err); }); });

但将来,请只使用某种 IDE。 即使只是将您的代码转储到JSFiddle 中也会为您突出显示这一点。

暂无
暂无

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

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