【问题标题】:It is showing that unexpected end of input as error, what should i do它显示输入意外结束为错误,我该怎么办
【发布时间】:2020-04-10 11:06:53
【问题描述】:
 const express = require('express')
const User = require('../models/User')
const auth = require('../middleware/auth')

const router = express.Router()

router.post('/users', async (req, res) => {
    // Create a new user
    try {
        const user = new User(req.body)
        const status = 1
        await user.save()
        const token = await user.generateAuthToken()
        res.status(201).send({status, user, token })
    } catch (error) {
        res.status(400).send(error)
    }
})

router.post('/users/login', async(req, res) => {
    //Login a registered user
    try {
        const { email, password } = req.body
        const user = await User.findByCredentials(email, password)
        const status = 1
        if (!user) {
            return res.status(401).send({error: 'Login failed! Check authentication credentials'})
        }
        const token = await user.generateAuthToken()
        res.send({ status,user,token })
    } catch (error) {
        res.status(400).send(error)
    }

})

router.get('/users/me', auth, async(req, res) => {
    // View logged in user profile
    res.send(req.user)
})

router.post('/users/me/logout', auth, async (req, res) => {
    // Log user out of the application
    try {
        req.user.tokens = req.user.tokens.filter((token) => {
            return token.token != req.token
        })
        await req.user.save()
        res.send()
    } catch (error) {
        res.status(500).send(error)
    }
})

router.post('/users/me/logoutall', auth, async(req, res) => {
    // Log user out of all devices
    try {
        req.user.tokens.splice(0, req.user.tokens.length)
        await req.user.save()
        res.send()
    } catch (error) {
        res.status(500).send(error)
    }
})
router.patch('/users/places/:id', auth, async (req, res) => {
const updates = Object.keys(req.body)
const allowedUpdates = ['places']
const isValidOperation = updates.every((update) => allowedUpdates.includes(update))

if (!isValidOperation) {
    return res.status(400).send({ error: 'Invalid updates!' })
}

try {
    const task = await Task.findOne({ _id: req.params.id})

    if (!task) {
        return res.status(404).send()
    }
    else{

    updates.forEach((update) => {
      task[update] = update === 'places' 
        ? task[update].map(places=>places.place === req.body[update].place ? req.body[update] : places)
        : req.body[update]
    })
    updates.forEach((update) => {
        task[update] = update === 'places' 
          ? task[update].map(places=>places.latitude === req.body[update].latitude ? req.body[update] : places)
          : req.body[update]
      })
      updates.forEach((update) => {
        task[update] = update === 'places' 
          ? task[update].map(places=>places.longitude === req.body[update].longitude ? req.body[update] : places)
          : req.body[update]
      })
    }
    await task.save()
    res.send(task)
} catch (e) {
    res.status(400).send(e)
})



module.exports = router
It is showing an error at line no 102
Unexpected end of input as error and I am not able to understand the error as I am new to the code in nodejs.
below given is the log of the yarn run v1.22.4
4:23:43 PM web.1 |  $ env-cmd -f ./.env node src/app.js
4:23:44 PM web.1 |  H:\Minor\Backend\src\ro
4:23:44 PM web.1 |  uters\user.js:106
4:23:44 PM web.1 |  module.exports = router
4:23:44 PM web.1 |  SyntaxError: Unexpected end of input
4:23:44 PM web.1 |      at wrapSafe (internal/modules/cjs/loader.js:1072:16)
4:23:44 PM web.1 |      at Module._compile (internal/modules/cjs/loader.js:1122:27)
4:23:44 PM web.1 |      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
4:23:44 PM web.1 |      at Module.load (internal/modules/cjs/loader.js:1002:32)
4:23:44 PM web.1 |      at Function.Module._load (internal/modules/cjs/loader.js:901:14)
4:23:44 PM web.1 |      at Module.require (internal/modules/cjs/loader
4:23:44 PM web.1 |  .js:1044:19)
4:23:44 PM web.1 |      at require (internal/modules/cjs/helpers.js:77:18)
4:23:44 PM web.1 |      at Object.<anonymous> (H:\Minor\Backend\src\app.js:3:20)
4:23:44 PM web.1 |      at Module._compile (internal/modules/cjs/loader.js:1158:30)
4:23:44 PM web.1 |      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
4:23:44 PM web.1 |  error Command failed with exit code 1.
4:23:44 PM web.1 |  info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[DONE] Killing all processes with signal  SIGINT
4:23:44 PM web.1 Exited with exit code nullterminal showing the error

上面给出的是在终端中运行的代码的日志 错误是 Unexpected token')' 请帮我解决这个错误 因为我是节点 js 的新手,所以我不太了解它,在谷歌上我也找不到任何解决方案...................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... .............................请检查我的代码一次并帮助我解决它。

【问题讨论】:

    标签: javascript node.js mongodb express


    【解决方案1】:

    忘记了

    })
    

    module.exports = router之前

    将您的代码放入其中,https://beautifier.io/ 并查看...

    更新:

    是的,看看最后的try catch:

        } catch (e) {
            res.status(400).send(e)
        }) // <--- remove the )
    })
    

    【讨论】:

    • 添加上述括号后,显示意外令牌错误}) 5:14:18 PM web.1 | ^ 下午 5:14:18 web.1 | SyntaxError: Unexpected token ')'
    • 看看你的代码,缩进它,然​​后检查括号......你没有打火机吗?如果您使用 blocnote 进行编码,我建议您使用任何类型的免费代码编辑器,它将突出显示代码,您将能够直接显示此类错误。我认为这是主要问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多