在我对我的项目运行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服务配置存储在server文件夹中的service-configuration.js文件下:
import { ServiceConfiguration } from 'meteor/service-configuration';
ServiceConfiguration.configurations.update(
{ service: 'google' },
{
$set: {
clientId: 'XXX',
loginStyle: 'popup',
secret: 'XXXX'
}
}
);我无法理解这个错误。在我运行meteor reset之前,它是起作用的。
发布于 2020-04-21 17:32:29
我已经想明白了。回答这个问题是为了帮助那些将来可能也遇到这个问题的人。
因为我做了meteor reset,这个项目就被重置为0。所以我需要将服务配置细节重新插入到mongo集合meteor_accounts_loginServiceConfiguration中.我想我无意中删除了我的server/main.js文件中的upsert命令。因此,在我完成meteor reset之后,服务配置是空的,并且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
}
}
);
});如果您想知道如何使用clientId和secret,请转到Google控制台这里,创建一个新项目,并相应地配置凭证和OAuth同意书屏幕。
https://stackoverflow.com/questions/61348150
复制相似问题