我不知道为什么我的代码没有编译,我有以下代码。
mongoose.connect(db)
.then( onFulfilled: () => console.log(`MongoDB connected.`))
.catch( onRejected: err => console.log(err));
这是我得到的错误
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
/home/sambulo/Documents/linkshortenner/server.js:8
.then( onFulfilled: () => console.log(`MongoDB connected.`))
^^^^^^^^^^^
SyntaxError: missing ) after argument list
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
答案 0 :(得分:1)
.then( onFulfilled: () => console.log(`MongoDB connected.`))
不能完全确定您要在此处执行的操作,似乎您是在then子句中声明了您实际想要具有功能的对象:
.then( () => console.log(`MongoDB connected.`) )
或:
.then( function onFulfilled () { console.log(`MongoDB connected.`) } )
编辑:同样适用于catch()
答案 1 :(得分:1)
只需删除onFulfilled
和onRejected
然后确保db
包含数据库地址。
您的最终代码应如下所示,
mongoose.connect(db)
.then(() => console.log(`MongoDB connected.`))
.catch((err) => console.log(err));
答案 2 :(得分:1)
Promise有两种方法then()
和catch()
,
Promise.then(() => {});
Promise.catch(() => {});
Promise
.then(() => {})
.catch(() => {});
在您的代码中,您需要删除:
并稍稍更新代码...
mongoose.connect(db)
.then((onFulfilled) => console.log(`MongoDB connected.`))
.catch((err) => console.log(err));