[英]TypeError: Cannot read property 'split' of null
I try to authentificate to connect to Google API in order to use Google Analytics.我尝试验证以连接到 Google API 以使用 Google Analytics。 Here is my code:
这是我的代码:
'use strict'
const fs = require('fs')
const open = require('open')
const Fs = require('fs')
const Google = require('googleapis')
const Url = require('url')
const Http = require('http')
const CREDENTIALS_DIR = './.credentials/'
const SECRET_PATH = CREDENTIALS_DIR + 'google-analytics-secret.json'
const TOKEN_PATH = CREDENTIALS_DIR + 'google-analytics-accesstoken.json'
let auth = loadCredentials()
const oauth2Client = new Google.auth.OAuth2(
auth.CLIENT_ID,
auth.CLIENT_SECRET,
auth.REDIRECT_URL)
function loadCredentials () {
try {
let secret = Fs.readFileSync(SECRET_PATH)
console.log('Credentials successfully loaded from file.')
return {
CLIENT_ID: JSON.parse(secret).web.client_id,
CLIENT_SECRET: JSON.parse(secret).web.client_secret,
REDIRECT_URL: JSON.parse(secret).web.redirect_uris[0]
}
}
catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
else {
console.log('Credentials successfully loaded from environment.')
return {
CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
REDIRECT_URL: 'http://localhost:1234/'
}
}
}
}
function getScopes () {
return [
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/analytics.readonly'
]
}
function getAuthCallbackServer () {
let url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: getScopes()
})
open(url, 'chrome')
let server = Http.createServer(handleAuthCallback)
server.listen(9999, () => {
console.log('Server listening on: http://localhost:1234')
})
}
// Parse Code sent by Google
function handleAuthCallback (req, res) {
let parse = Url.parse(req.url)
let code = parse.query.split('=').pop()
console.log(`Got code: ${code}`)
res.writeHead(200)
res.end('Awesome... Got the Code')
}
getAuthCallbackServer()
I try to make it modular with the use of functions and I get the following error (inside the handleAuthCallback function) :我尝试使用函数使其模块化,但出现以下错误(在 handleAuthCallback 函数内):
TypeError: Cannot read property 'split' of null
This is the object I got when I try to print parse
in the handleAuthCallback function:这是我尝试在 handleAuthCallback 函数中打印
parse
时得到的对象:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?code=x/xxxxxxxxxxxxxxxxxxxxxxxx',
query: 'code=x/xxxxxxxxxxxxxxxxxxxxxxxx',
pathname: '/',
path: '/?code=x/xxxxxxxxxxxxxxxxxxxxxxxx',
href: '/?code=x/xxxxxxxxxxxxxxxxxxxxxxxx' }
I don't really understand this error as it was working before I refactored my code.我不太明白这个错误,因为它在我重构我的代码之前就起作用了。 Any advice on where I could be wrong ?
关于我哪里可能出错的任何建议?
Thanks.谢谢。
This function
is being called:正在调用此
function
:
// Parse Code sent by Google
function handleAuthCallback (req, res) {
let parse = Url.parse(req.url)
let code = parse.query.split('=').pop()
console.log(`Got code: ${code}`)
res.writeHead(200)
res.end('Awesome... Got the Code')
}
when a callback occurs specified here:此处指定的回调发生时:
let server = Http.createServer(handleAuthCallback)
Url
is defined here: Url
在此处定义:
const Url = require('url')
parse is the result of Url.parse(req.url)
. parse 是
Url.parse(req.url)
的结果。 This does not have a query property, so parse.query
is undefined
and therefore parse.query.split
will yield error, it is possible that when you made some refactoring, the code which defined query
was removed.这没有查询属性,所以
parse.query
是undefined
,因此parse.query.split
会产生错误,有可能当你进行一些重构时,定义query
的代码被删除了。
Assuming query
is object, try this:假设
query
是对象,试试这个:
let parse = Url.parse(req.url);
let code = parse.query.code;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.