Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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编译有问题_Javascript_Node.js_Express - Fatal编程技术网

javascript编译有问题

javascript编译有问题,javascript,node.js,express,Javascript,Node.js,Express,我有一个应用程序Javascript文件,它在VS Studio代码的控制台部分显示了一个错误,它一直在说:app.js:解析器希望在这里找到一个“}”来匹配“{”标记。控制台将不会编译 根据我的代码,你能告诉我在我的大括号里我需要关闭在哪里吗?我想我可能会感到困惑 以下是我的app.js代码: const express = require('express'); const app = express(); const bodyParser = require('body-parser')

我有一个应用程序Javascript文件,它在VS Studio代码的控制台部分显示了一个错误,它一直在说: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=>{ //调用mongoose方法find MongoDB.Students.find Item.find //如果返回数据,则将数据作为响应发送 .thendata=>res.status200.jsondata //如果出现错误,则发送内部服务器错误 .catcherr=>{ log'Error:${err}'; res.status500.jsoner; };
};谢谢Jacob,修复了红色突出显示的编码语法错误。但现在我遇到了错误TS1086:在环境上下文中无法声明访问器。当我尝试编译时。你知道这意味着什么吗?是的,它说24 get enabled:boolean;但这是在多行上发生的,我刚才在谷歌上搜索,显然这可能是be Angular版本有一个问题,不确定是否是这样。由于没有任何代码段在其中启用或启用,我假设它在“models/item”中,但是,最好尝试一些事情,并为此单独提出一个问题。