【问题标题】:Unexpected token syntax error I can not see我看不到意外的令牌语法错误
【发布时间】:2018-02-12 20:39:04
【问题描述】:

感谢您的观看。这是我得到的语法错误:

[0] /Users/alexkarasik/Documents/server/services/passport.js:26
[0] async (accessToken, refreshToken, profile, done) => {
[0]       ^
[0] SyntaxError: Unexpected token (

这是错误引用的文件。我已经上下查找了 2 多个小时,并且认为没有理由收到此错误:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id)
    .then(user => {
      done(null, user);
    })
});

passport.use(
  new GoogleStrategy({
    clientID: keys.googleClientID,
    clientSecret: keys.googleClientSecret,
    callbackURL: '/auth/google/callback',
    proxy: true
  },
async (accessToken, refreshToken, profile, done) => {
    const existingUser = await User.findOne({ googleId: profile.id });

    if (existingUser){
            //we already have a record with the give profileId
            return done(null, existingUser);
    }
            // we don't have a user record with this ID, make a new record
      const user = await new User({ googleId: profile.id }).save();
      done(null, user);
    }
  )
);

非常感谢任何意见。

【问题讨论】:

  • 您的服务器是否完全支持 ESnext? (尤其是异步等待..)
  • @Jonasw 可能是对的。你有 webpack 配置文件或 babelrc 设置吗?
  • @sourRasperri webpack?这与服务器端代码有什么关系?如果你可以简单地更新 bodejs,为什么还要一个 babel?
  • 您可以使用类似这样的方式返回到 Promise 处理:(accessToken, refreshToken, profile, done) => { User.findOne({ googleId : profile.id }).exec().then( (existingUser)=>{ /* [...] */ } ); }
  • 你在什么环境下尝试运行这段代码,究竟是什么引发了这个错误?

标签: javascript syntax-error react-server


【解决方案1】:

Node 没有针对新的 ES6 语法正确更新。更新它解决了这个问题。谢谢您的帮助。

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2017-10-07
    相关资源
    最近更新 更多