【发布时间】:2018-02-12 07:09:42
【问题描述】:
我一直在关注 this 使用 GraphQL 的教程,它告诉我在我的 src/index.js 文件中编写这段代码:
const express = require('express');
const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');
const schema = require('./schema');
// 1
const connectMongo = require('./mongo-connector');
// 2
const start = async () => {
// 3
const mongo = await connectMongo();
var app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({
context: {mongo}, // 4
schema
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Hackernews GraphQL server running on port ${PORT}.`)
});
};
// 5
start();
虽然当我尝试使用 node ./src/index.js 运行代码时,它给了我这个错误:
const start = async () => {
^
SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
我在网上搜索过,似乎 if 可能是由 here 的 node.js 版本引起的,但我检查了我的 noed.js 版本,它是 8.3.0 所以应该支持它而不必使用如果我没记错的话,巴别塔。
这是我的package.json 文件:
{
"name": "graphql-js-tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^1.1.2",
"body-parser": "^1.17.2",
"express": "^4.15.4",
"graphql": "^0.11.2",
"graphql-tools": "^1.2.2",
"mongodb": "^2.2.31"
}
}
【问题讨论】:
-
async被视为函数名,箭头函数不接受名称。 -
@Teemu - 你确定吗?
-
哪个版本的nodejs
-
@Teemu - 我现在明白了 - 我确实从语法中假设意图是使用(糟糕)
async函数 - 似乎确实如此 -
@YellowPillow 不要使用 Homebrew 来安装 Node.js,它只会带来悲伤。使用官方安装程序。
标签: javascript node.js asynchronous