简体   繁体   中英

SyntaxError: Unexpected identifier Node.js

I'm making a simple web application and keep getting this error.

app.get("/campground/:id/comments/new",function(req,res){
    camp.findById(req.params.id)({
        if(err)
            console.log(err);
        else        
            res.render("comments/new",{campground:camp});
    });
});

and keep getting this error

 console.log(err);
 ^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

If I remove error Handling,I get the error,

res.render("comments/new",{campground:camp});
   ^

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Can Anyone Help?

You're not using findById properly, it should have a callback function, something like this:

app.get("/campground/:id/comments/new",function(req, res, err){
    camp.findById(req.params.id, function(err, camp) {
        if(err) {
            console.log(err);
        } else {   
            res.render("comments/new",{campground:camp});
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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