I want to check if email ID already exists in MongoDB. When if req.body.email is set then it would fire a MongoDB query and if result is true then it should send status:true in json.
I have tried sending res.json() from outside User.findOne() still it did not work. I have set Accept header to application/json. Still not working.
Code at 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()
}
})
Code in front end
fetch('/individual/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
email: "[email protected]"
})
}).then(function (res) {
return res.json()
}).then(function (data) {
console.log("received data: " + JSON.stringify(data))
})
error log
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 [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 info lifecycle [email protected]~start: [email protected]
7 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~start: PATH: <long path value>;
9 verbose lifecycle [email protected]~start: CWD: E:\Projects\codex-app
10 silly lifecycle [email protected]~start: Args: [ '/d /s /c', 'node ./bin/start' ]
11 silly lifecycle [email protected]~start: Returned: code: 1 signal: null
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start:node ./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 [email protected]
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 [email protected] start:node ./bin/start
22 error Exit status 1
23 error Failed at the [email protected] start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
It should either return {status: true} if found else {status: false}
res.json()should not be followed with.end(). It sends a request all on its own. I don't know if that's related to your issue or not. - jfriend00.catch()handler on yourfetch()and you specifically logerron your database call. If you're getting an error back from thefetch(), you will do nothing. - jfriend00npm ERR! code ELIFECYCLE npm ERR! errno 1- Ishwar Sarade