如何在调用res.json()时阻止Node.js应用失败?

时间:2019-07-19 03:33:20

标签: node.js express mongoose routes

我想检查MongoDB中是否已经存在电子邮件ID。如果设置了req.body.email,则将触发MongoDB查询,如果结果为true,则应在json中发送status:true。

我尝试从User.findOne()外部发送res.json()仍然无法正常工作。我已经将Accept标头设置为application / json。仍然无法正常工作。

NodeJS上的代码

router.post("/individual/signup", (req, res, next) => {
    let isJSON = false;
    if (req.body.email) {
        let email = req.body.email.toLowerCase().trim()
        User.findOne({ email: email }, (err, result) => {
            if (result) {
                return res.json({status: true}).end()
            } else {
                return res.json({status: false}).end()
            }
        })
    } else {
        return res.json({ status: false }).end()
    }
})

前端代码

fetch('/individual/signup', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    body: JSON.stringify({
        email: "ishwar@gmail.com"
    })
}).then(function (res) {
    return res.json()
}).then(function (data) {
    console.log("received data: " + JSON.stringify(data))
})

错误日志

0 info it worked if it ends with ok 1 verbose cli [ 1 verbose cli 'C:\\Program Files (x86)\\nodejs\\node.exe', 1 verbose cli 'C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', 1 verbose cli 'start' 1 verbose cli ] 2 info using npm@6.9.0 3 info using node@v12.0.0 4 verbose run-script [ 'prestart', 'start', 'poststart' ] 5 info lifecycle codex-app@0.0.0~prestart: codex-app@0.0.0 6 info lifecycle codex-app@0.0.0~start: codex-app@0.0.0 7 verbose lifecycle codex-app@0.0.0~start: unsafe-perm in lifecycle true 8 verbose lifecycle codex-app@0.0.0~start: PATH: <long path value>; 9 verbose lifecycle codex-app@0.0.0~start: CWD: E:\Projects\codex-app 10 silly lifecycle codex-app@0.0.0~start: Args: [ '/d /s /c', 'node ./bin/start' ] 11 silly lifecycle codex-app@0.0.0~start: Returned: code: 1 signal: null 12 info lifecycle codex-app@0.0.0~start: Failed to exec start script 13 verbose stack Error: codex-app@0.0.0 start:节点./bin/start 13 verbose stack Exit status 1 13 verbose stack at EventEmitter.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16) 13 verbose stack at EventEmitter.emit (events.js:196:13) 13 verbose stack at ChildProcess.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14) 13 verbose stack at ChildProcess.emit (events.js:196:13) 13 verbose stack at maybeClose (internal/child_process.js:1000:16) 13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:267:5) 14 verbose pkgid codex-app@0.0.0 15 verbose cwd E:\Projects\codex-app 16 verbose Windows_NT 10.0.18362 17 verbose argv "C:\\Program Files (x86)\\nodejs\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" 18 verbose node v12.0.0 19 verbose npm v6.9.0 20 error code ELIFECYCLE 21 error errno 1 22 error codex-app@0.0.0 start:节点./bin/start 22 error Exit status 1 23 error Failed at the codex-app@0.0.0 start script. 23 error This is probably not a problem with npm. There is likely additional logging output above. 24 verbose exit [ 1, true ]

如果找到则返回{status:true},否则返回{status:false}

1 个答案:

答案 0 :(得分:0)

我必须清理缓存,然后删除node_modules,删除package-lock.json,然后进行npm install

npm cache clean --force && rm -rf node_modules && rm ./package-lock.json && npm install