【问题标题】:Meteor's ServiceConfiguration broken after Meteor ResetMeteor 重置后 Meteor 的 ServiceConfiguration 损坏
【发布时间】:2020-04-22 01:32:29
【问题描述】:

在我的项目上运行meteor reset 后发生此错误

Uncaught TypeError: Cannot read property 'findOne' of undefined
at onLoginWithGoogle (Heading.js:23)
at Button._this.handleClick (modules.js?hash=aa2df6fbe7f4a6a52d262a213d0cfff2a56dcdc2:10098)
at HTMLUnknownElement.callCallback (modules.js?hash=aa2df6fbe7f4a6a52d262a213d0cfff2a56dcdc2:32249)
...

这是调用 ServiceConfiguration 的文件:

Heading.js

import React, { useContext, useState } from 'react'
import { Meteor } from 'meteor/meteor';
import ServiceConfiguration from 'meteor/service-configuration'
...

function Heading(props){
    const context = useContext(Context);
    const [error, setError] = useState('');

    const onLoginWithGoogle = () => {
        const {scope} = ServiceConfiguration.configurations.findOne({service: 'google'}); //this is where it failed

        Meteor.loginWithGoogle(
          {requestPermissions: scope, requestOfflineToken: true },
          error => {
            if (error) {
              if (error.errorType === 'Accounts.LoginCancelledError') return;
              alert('Login error', error);
            } else {
              //
            }
          }
        );
    };
}
//export

服务配置存储在service-configuration.js文件下的server文件夹中:

import { ServiceConfiguration } from 'meteor/service-configuration';

ServiceConfiguration.configurations.update(
  { service: 'google' },
  {
    $set: {
      clientId: 'XXX',
      loginStyle: 'popup',
      secret: 'XXXX'
    }
  }
);

我无法理解这个错误。在我运行 meteor reset 之前它就可以工作了。

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我想通了。回答这个问题是为了帮助将来可能也遇到这个问题的其他人。

    因为我做了meteor reset,所以项目被重置为0。所以我需要将服务配置详细信息重新插入到mongo集合meteor_accounts_loginServiceConfiguration...我想我不小心删除了server/main.js文件上的upsert命令。所以在我做了meteor reset之后,serviceconfiguration是空的,Meteor.startup()中没有代码来补充所需的细节。

    这是应该保留在server/main.js 中的代码:

    Meteor.startup(() => {
      // first, remove configuration entry in case service is already configured
      Accounts.loginServiceConfiguration.remove({
          service: "google"
        });
      Accounts.loginServiceConfiguration.upsert(
        { service: 'google' },
        {
          $set: {
            clientId: 'XXX', // change this to your actual clientId
            loginStyle: 'popup',
            secret: 'XXX' //change this to your actual secret
          }
        }
      );
    
    });
    

    如果您想知道如何使用 clientIdsecret,请前往 Google API 控制台 here,创建一个新项目,并相应地配置凭据和 OAuth 同意屏幕。

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2014-05-29
      相关资源
      最近更新 更多