mongoose Unexpected identifier

不明白为什么加了await userInfo.save();就报Unexpected identifier错误,实在找不出问题点,求大神相助,谢谢!
const config = require('../config')
const axios = require('axios')
const mongoose = require('mongoose')
const User = mongoose.model('User')
// console.log('User');
// console.log(User);
module.exports = async function (code) {

var url = [
  config.wechat,
  '?appid=',
  config.appId,
  '&secret=',
  config.appSecret,
  '&js_code=',
  code,
  '&grant_type=authorization_code'
].join('')

axios.get(url)
  .then(function(response){
    const userInfo = new User({
      sessionKey:response.data.session_key,
      openid:response.data.openid
    })
    console.log('userInfo');
    console.log(userInfo);
    await userInfo.save();
  })
  .catch(function(error){
    console.log(error);
  });

}

阅读 2.5k
2 个回答

楼主,你好!你写的确实有点问题。
改成如下代码,试试看~

.....
axios.get(url)
    // 这里要加 async,可以去搜下 async...await 的相关文章 
  .then( async function(response){
    const userInfo = new User({
      sessionKey:response.data.session_key,
      openid:response.data.openid
    })
    console.log('userInfo');
    console.log(userInfo);
    await userInfo.save();
  })
  .catch(function(error){
    console.log(error);
  });

如有帮助,麻烦点击下采纳,谢谢~

新手上路,请多包涵

非常感谢,是我async没理解透彻

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题