Firebase Tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var { google } = require("googleapis"); | |
var request = require("request"); | |
var serviceAccountDir = "./serviceAccountKey.json" | |
generateAccessToken(serviceAccountDir).then(access_token => { | |
request({ | |
url: "https://database-cua-ban.firebaseio.com/path/nao/do.json", | |
qs: { | |
shallow: "true", | |
access_token: access_token | |
} | |
}, (err, res, data) => { | |
console.log(data) | |
}) | |
}) | |
/** | |
* Tạo access_token từ serviceAccountKey.json | |
* @param {String} dir Đường dẫn đến file .json | |
* @returns {Promise} Resolve access_token | |
*/ | |
function generateAccessToken(dir) { | |
return new Promise(resolve => { | |
// Load the service account key JSON file. | |
var serviceAccount = require(dir); | |
// Define the required scopes. | |
var scopes = [ | |
"https://www.googleapis.com/auth/userinfo.email", | |
"https://www.googleapis.com/auth/firebase.database" | |
]; | |
// Authenticate a JWT client with the service account. | |
var jwtClient = new google.auth.JWT( | |
serviceAccount.client_email, | |
null, | |
serviceAccount.private_key, | |
scopes | |
); | |
// Use the JWT client to generate an access token. | |
jwtClient.authorize(function(error, tokens) { | |
if (error) { | |
console.error("Error making request to generate access token:", error); | |
} else if (tokens.access_token === null) { | |
console.error("Provided service account does not have permission to generate access tokens"); | |
} else { | |
resolve(tokens.access_token); | |
} | |
}); | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For example: | |
// generateSearchIndex("Nguyen Xuan Son") | |
// returns ["Ngu", "Nguy", "Nguye", "Nguyen", "Xua", "Xuan", "Son"] | |
function generateSearchIndex(str) { | |
var temp = [] | |
str.trim().split(" ").forEach(word => { | |
temp = temp.concat(generateSearchIndexWord(word)) | |
}) | |
return temp | |
} | |
function generateSearchIndexWord(word) { | |
if (word.length == 0) { | |
return [] | |
} else if (word.length <= 3) { | |
return [word] | |
} else { | |
var ret = [] | |
for (var i = 3 ; i <= word.length ; i++) { | |
ret.push(word.substring(0, i)) | |
} | |
return ret | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Đây chỉ là code mẫu, mang tính tham khảo | |
var db = firebase.database() | |
// Danh sách id có thể lấy bằng cách dùng code từ mục 2 | |
var ids = { | |
"12417221992xxxxx": true, | |
"16558810177xxxxx": true, | |
"16030810531xxxxx": true, | |
"22281263838xxxxx": true | |
} | |
var promises = Object.keys(ids).map(id => { | |
return db.ref("/user/"+id+"/gender").once("value") | |
}) | |
Promise.all(promises).then(snapshots => { | |
var data = snapshots.map(snapshot => { | |
return { | |
id: snapshot.key, | |
gender: snapshot.val() | |
} | |
}) | |
/* | |
data giờ sẽ thành | |
[ | |
{ id: "12417221992xxxxx", gender: "female" }, | |
{ id: "16558810177xxxxx", gender: "male" }, | |
{ id: "16030810531xxxxx", gender: "female" }, | |
{ id: "22281263838xxxxx", gender: "male" } | |
] | |
*/ | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment