Webstorm 2018.3/Windows 10
v8.0.0/6.4.1
JetBrains ECMAScript6 in Webstorm Babel FileWatcher
我通过以下步骤添加了Babel
对于本地存储库,我安装了npm i --save-dev @babel/cli @babel/core @babel/preset-env @babel/plugin-object-proposed-rest-spread
在根文件夹中添加了.babelrc
,内容如下:
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-object-rest-spread"]
}
为npm start
配置了运行配置,因为我的package.json
中有"start": nodemon app.js
。
我正在使用mongoose
存储和从MongoDB获取信息,并使用GraphQL为查询/变异提供端点
这是我代码中的mutation
(函数)之一:
createEvent: (args) => {
const event = new Event({
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: new Date(args.eventInput.date)
});
return event
.save()
.then(result => {
console.log(result);
return { ...result._doc };
})
.catch(err => {
console.log(err);
throw err;
});
}
错误:
SyntaxError: Unexpected token ...
根据SE Query to Configure Babel with Webstorm for node.js project,我尝试创建一个node.js app
,但现在出现错误:
Error: querySrv ENOTFOUND _mongodb._tcp.undefined-p8e7q.mongodb.net
at errnoException (dns.js:50:10)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:244:19)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'querySrv',
hostname: '_mongodb._tcp.undefined-p8e7q.mongodb.net'
我现在将{ ...result._doc }
替换为return result._doc
,代码可以正常工作,但是我希望在其他代码块中使用散布运算符。
我知道该错误与Webstorm
无关,但是我看不到该问题的其他替代方法
Node.js配置
通过右键单击app.js
文件和菜单中的Create 'app.js'
创建。我安装了npm i --save-dev @babel/register
,并将其更改为-r @babel/register
,如博客中所述
app.js
const express = require('express'); // Add Express Module
const bodyParser = require('body-parser'); // Add Body-Parser Middleware for JSON handling in Requests
const graphqlHttp = require('express-graphql'); // Add Middleware for GraphQL Resolvers over Express HTTP
const { buildSchema } = require('graphql'); // Javascript Object-Destructuring (pull objects from packages)
const mongoose = require('mongoose'); // MongoDB Third-Party package
const Event = require('./models/event'); // MongoDB Event Model
const app = express();
app.use(bodyParser.json()); // JSON parsing Middleware added
app.use('/graphql', graphqlHttp({
schema: buildSchema(`
type Event {
_id: ID!
title: String!
description: String!
price: Float!
date: String!
}
input EventInput {
title: String!
description: String!
price: Float!
date: String!
}
type RootQuery {
events: [Event!]!
}
type RootMutation {
createEvent(eventInput: EventInput): Event
}
schema {
query: RootQuery
mutation: RootMutation
}
`),
rootValue: {
events: () => {
return Event.find()
.then(events => {
return events.map( event => {
event._doc._id = event.id;
return event._doc;
});
})
.catch(err => {
console.log(err);
throw err;
})
},
createEvent: (args) => {
const event = new Event({
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: new Date(args.eventInput.date)
});
return event
.save()
.then(result => {
console.log(result);
return { ...result._doc };
// return result._doc
})
.catch(err => {
console.log(err);
throw err;
});
}
},
graphiql: true
}));
mongoose.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER_NAME}-p8e7q.mongodb.net/${process.env.MONGO_DB_NAME}?retryWrites=true`)
.then( () => {
app.listen(3000);
}).catch(err => {
console.log(err);
});
package.json
我添加了一些怪异的babel软件包,以进行一些迫切的尝试,以使其启动并运行,这些软件包位于dev-dependencies
{
"name": "graphql-express-app",
"version": "0.0.4",
"description": "Academind's YouTube Series on GraphQL-Express-React",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"keywords": [
"graphql",
"express",
"node.js"
],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/preset-stage-2": "^7.0.0",
"babel-preset-stage-2": "^6.24.1",
"body-parser": "^1.18.3",
"express": "^4.16.4",
"express-graphql": "^0.7.1",
"graphql": "^14.0.2",
"mongoose": "^5.4.1"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/register": "^7.0.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"nodemon": "^1.18.9"
}
}
答案 0 :(得分:1)
我精简了您的示例,并添加了启动和运行它所需的最低版本。
基本上,您缺少的是babel实际去编译代码的部分。我不知道webstorm是否为您做到了,我正在使用VS代码。
.babelrc :
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-proposal-object-rest-spread"]
}
package.json (仅使用deps和脚本,其余内容省略):
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --exec babel-node app.js"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/node": "^7.2.2",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"nodemon": "^1.18.9"
}
最小 app.js :
const o = {
a: 'a',
b: 'b',
};
const o2 = { ...o };
console.log(o2);
// Outputs: { a: 'a', b: 'b' }
您可以使用最新版本来避免不得不移植nodejs代码的麻烦。从8.6.0版开始的节点中支持扩展运算符:https://node.green/#ES2018-features-object-rest-spread-properties
希望有帮助!