I am trying to import a json file to firestotre using javascript but I am getting this error when I do node import.js, :
Here is my import.js file:
// Imports
const firestoreService = require('firestore-export-import');
const firebaseConfig = require('./config.js');
const serviceAccount = require('./serviceAccount.json');
// JSON To Firestore
const jsonToFirestore = async () => {
try {
console.log('Initialzing Firebase');
await firestoreService.initializeApp(serviceAccount, firebaseConfig.databaseURL);
console.log('Firebase Initialized');
await firestoreService.restore('./carapp-96076-default-rtdb-export.json');
console.log('Upload Success');
}
catch (error) {
console.log(error);
}
};
jsonToFirestore();
These are all the files I have in my git repository.
What am I doing wrong? Please Help!
My Json file looks like this:
It looks like your json structure is not in the required format as shown in the link of firestore-export-import
Here I have recreated the setup but with my custom json file:
const { initializeFirebaseApp, restore } = require('firestore-export-import')
const firebaseConfig = require('./config.js');
const serviceAccount = require('./serviceAccountKey.json')
const admin = require('firebase-admin');
console.log('Initializing Firebase');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: firebaseConfig.databaseURL
});
console.log('Firebase Initialized');
// JSON To Firestore
const jsonToFirestore = async () => {
try {
console.log('Upload Started');
await restore('./data.json');
console.log('Upload Success');
}
catch (error) {
console.log(error);
}
};
jsonToFirestore();
Where my data.json is structured as follows:
{
"test": {
"Hobart": { "id": 1, "first_name": "Hobart" },
"Mitzi": { "id": 2, "first_name": "Mitzi" }, //… like documents
}
}
This has successfully restored the json file into firestore collection of test having Hobart , Mitzi , etc as document ids and if you want to generate auto-incremented ids then you can just replace outside curly brackets to boxy brackets as shown in this link.
Then json structure for storing documents as auto-generated ids will be as follows:
{
"test": [
{ "id": 1, "first_name": "Hobart" },
{ "id": 2, "first_name": "Mitzi" }, //… like documents
]
}
Related
i'm just trying to this simply.
Convert and object to a JSON format.
Write a file & that Object to JSON data in that new file.
Read from that newly created file.
Write the read data to another new file
PROBLEM:
Some kind of error with data argument of readFile function but i don't know how to resolve it. (Exactly the reason i'm asking this question)
NODE.JS CODE:
const fs = require('fs')
const object1 = {
name: "Zaeem Javed",
age: 21,
profession: "Web Application Developer"
}
const jsonData = JSON.stringify(object1)
fs.writeFile("jsonDataFile.json", jsonData, (err) => {
console.log(err)
})
const readJSON = fs.readFile("jsonDataFile.json", "utf-8", (err, data) => {
console.log(data)
})
fs.writeFile("readJSON.txt", readJSON, (err) => {
console.log(err)
})
This code solved my problem:
const fs = require('fs')
const object1 = {
name: "Zaeem Javed",
age: 21,
profession: "Web Application Developer"
}
const jsonData = JSON.stringify(object1)
fs.writeFile("jsonDataFile.json", jsonData, (err) => {
console.log("Write Successful")
})
fs.readFile("jsonDataFile.json", "UTF-8",(err, data) => {
const readJSON = data
fs.writeFile("readJSON.json", readJSON, (err) => {
console.log(err)
})
})
Apologies in-advance for the long post.So currently, my JSON is coming out as seen below, and I only want to return the name value of the items in Filter 1, with the corresponding category name. What is the best way for me to do so?
I assume this is a backend fix, so I have also included my backend below. Any help would be appreciated, as I often don't work with JSON.
{
"urls":{
“Url1”:{
"status":200
}
},
“Filter1”:[
{
"slug”:”slug1,
"name”:”name1”,
"confidence":100,
"version":null,
"icon”:”icon1”,
"website”:”website1”,
"cpe":"cpe1”,
"categories":[
{
"id":22,
"slug”:”slug2”,
"name”:”name3”
}
]
},
{
"slug”:”slug3”,
"name”:”name4”,
"confidence":100,
"version":null,
"icon”:”icon2”,
"website”:”website2”,
"cpe":null,
"categories":[
{
"id":30,
"slug”:”slug4”,
"name”:”name5”
},
{
"id":75,
"slug”:”slug5”,
"name”:”name6”
}
]
}
]
}
Backend:
app.get('/',(req,res)=>{
res.render('index',{
data: 'name'
})
})
app.post('/', async(req, res) => {
const whoissearch = new whoissearch()
try {
await whoissearch()
const url = req.body
let urlEncoded = decodeURIComponent(url.url);
await whoissearch.init();
const site = await whoissearch.open(urlEncoded)
site.on('error', console.error) // catch the errors
const results = await site.analyze()
const data = JSON.stringify(results, null, 2)
console.log(results.whois[0].name)
await whoissearch.destroy()
res.render('index', {
data: data
});
Sample of how it is being shown:
I am building an App using React Native and Firebase, and I want to test Delete function using jest/firestore-jest-mock.
My Problem is when I try this query it always return True.
"devDependencies": {
"#babel/core": "~7.9.0",
"firestore-jest-mock": "^0.7.1",
"jest-expo": "^40.0.1",
},
Here is my code:
// Fake DATABASE
mockFirebase({
database: {
categories_costs: [{ id: 'key1',title: 'test1', icon: 'icontest1' }],
},
});
const firebase = require('firebase');
const db = firebase.firestore();
// test with wrong ID
test('Delete Document In categories_income', () => {
return db.collection('categories_costs')
.doc('keyNoExist')
.delete()
.then(() => {
//Always get into this part
console.log("Document successfully deleted!");
}).catch((error) => {
console.error("Error removing document: ", error);
});
});
The test passed correctly like this although the id is wrong.
This is my solution for it, in this part it check if the document exist and then remove it (the fake data).
test('Delete categories_income Bykey', () => {
const ref = db.collection('categories_income').doc('key1');
return ref.get().then((doc) => {
if (doc.exists) {
ref.delete().then(() => {
expect(mockCollection).toHaveBeenCalledWith('categories_income');
}).catch((error) => {
expect(error).toEqual('error')
});
}else{
throw new Error("Document Does Not Exist")
}
});
});
When sending an array to Firebase Realtime Database, I find that it always formats it like a number and not an array.
Im using Firebase Realtime Database with Node.js to send the array.
This is the portion that sends the array if it does not exist
admin.database().ref(`BasicUserData/${msg.author.id}`).set({
userId: msg.author.id,
lastUser: msg.author.tag,
pastUsers: [msg.author.tag], // HERE
lastAvatar: avatarUrl,
pastAvatars: [avatarUrl], // HERE
lastDiscrim: msg.author.discriminator,
pastDiscrims: [msg.author.discriminator], // HERE
userCreated: msg.author.createdAt.toDateString()
});
Here is the entire Discord.js command plus some variables filled in for context:
const admin = require("firebase-admin");
const firebase = require("firebase");
const serviceAccount = require("(REMOVED).json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://(REMOVED).firebaseio.com"
});
{ ... }
bot.on('message', msg => { // Discord.JS
{ ... }
// !updateUser
case `updateuser`: {
try {
let avatarUrl = `https://cdn.discordapp.com/avatars/${msg.author.id}/${msg.author.avatar}.png`;
admin.database().ref(`BasicUserData/${msg.author.id}`).on("value", (val) => {
if (val.hasChild("lastUser")) {
var data = val.val();
var pastUsernames = data.pastUsers;
var pastAvatarURLS = data.pastAvatars;
var pastDiscriminators = data.pastDiscrims;
console.info(pastUsernames);
admin.database().ref(`BasicUserData/${msg.author.id}`).set({
userId: msg.author.id,
lastUser: msg.author.tag,
pastUsers: pastUsernames.push(msg.author.tag),
lastAvatar: avatarUrl,
pastAvatars: pastAvatarURLS.push(avatarUrl),
lastDiscrim: msg.author.discriminator,
pastDiscrims: pastDiscriminators.push(msg.author.discriminator),
userCreated: msg.author.createdAt.toDateString()
});
} else {
admin.database().ref(`BasicUserData/${msg.author.id}`).set({
userId: msg.author.id,
lastUser: msg.author.tag,
pastUsers: [msg.author.tag],
lastAvatar: avatarUrl,
pastAvatars: [avatarUrl],
lastDiscrim: msg.author.discriminator,
pastDiscrims: [msg.author.discriminator],
userCreated: msg.author.createdAt.toDateString()
});
}
});
const dataEmbed = new Discord.MessageEmbed()
.setColor('#f4be25')
.setTitle(`Saving ${msg.author.tag.split("#")[0]}`)
.addField('Saving Discriminator', msg.author.discriminator, true)
.addField('Saving Username', `${msg.author.tag}`, true)
.addField('Saving Creation Date', msg.author.createdAt.toDateString(), true)
.setThumbnail(avatarUrl);
msg.channel.send(dataEmbed);
} catch (e) {
msg.channel.send(`${e}`);
}
break;
}
If anyone could help me out, I would appreciate it.
I am referencing this tutorial for Firestore security rules. I have extracted the code from the repository and it matches that of the video.
I changed the setup code to run the firestore.rules instead of firestore-test.rules, and tried running firebase emulators:start and jest ./spec following the same directory structure, I fail the tests of "should allow delete when user is admin" and "should not allow delete for normal user" and the reason it is failing is due to the write rule in the wildcard. Does anyone know what is wrong?
collections.spec.js
const { setup, teardown } = require("./helpers");
describe("General Safety Rules", () => {
afterEach(async () => {
await teardown();
});
test("should deny a read to the posts collection", async () => {
const db = await setup();
const postsRef = db.collection("posts");
await expect(postsRef.get()).toDeny();
});
test("should deny a write to users even when logged in", async () => {
const db = await setup({
uid: "danefilled"
});
const usersRef = db.collection("users");
await expect(usersRef.add({ data: "something" })).toDeny();
});
});
describe("Posts Rules", () => {
afterEach(async () => {
await teardown();
});
test("should allow update when user owns post", async () => {
const mockData = {
"posts/id1": {
userId: "danefilled"
},
"posts/id2": {
userId: "not_filledstacks"
}
};
const mockUser = {
uid: "danefilled"
};
const db = await setup(mockUser, mockData);
const postsRef = db.collection("posts");
await expect(
postsRef.doc("id1").update({ updated: "new_value" })
).toAllow();
await expect(postsRef.doc("id2").update({ updated: "new_value" })).toDeny();
});
test("should allow delete when user owns post", async () => {
const mockData = {
"posts/id1": {
userId: "danefilled"
},
"posts/id2": {
userId: "not_filledstacks"
}
};
const mockUser = {
uid: "danefilled"
};
const db = await setup(mockUser, mockData);
const postsRef = db.collection("posts");
await expect(postsRef.doc("id1").delete()).toAllow();
await expect(postsRef.doc("id2").delete()).toDeny();
});
test("should allow delete when user is admin", async () => {
const mockData = {
"users/filledstacks": {
userRole: "Admin"
},
"posts/id1": {
userId: "not_matching1"
},
"posts/id2": {
userId: "not_matching2"
}
};
const mockUser = {
uid: "filledstacks"
};
const db = await setup(mockUser, mockData);
const postsRef = db.collection("posts");
await expect(postsRef.doc("id1").delete()).toAllow();
});
test("should not allow delete for normal user", async () => {
const mockData = {
"users/filledstacks": {
userRole: "User"
},
"posts/id1": {
userId: "not_matching1"
},
"posts/id2": {
userId: "not_matching2"
}
};
const mockUser = {
uid: "filledstacks"
};
const db = await setup(mockUser, mockData);
const postsRef = db.collection("posts");
await expect(postsRef.doc("id1").delete()).toDeny();
});
test("should allow adding a post when logged in", async () => {
const db = await setup({
uid: "userId"
});
const postsRef = db.collection("posts");
await expect(postsRef.add({ title: "new_post" })).toAllow();
});
test("should deny adding a post when not logged in", async () => {
const db = await setup();
const postsRef = db.collection("posts");
await expect(postsRef.add({ title: "new post" })).toDeny();
});
});
firestore.rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// lock down the db
match /{document=**} {
allow read: if false;
allow write: if false;
}
match /posts/{postId} {
allow update: if userOwnsPost();
allow delete: if userOwnsPost() || userIsAdmin();
allow create: if loggedIn();
}
function loggedIn() {
return request.auth.uid != null;
}
function userIsAdmin() {
return getUserData().userRole == 'Admin';
}
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data
}
function userOwnsPost() {
return resource.data.userId == request.auth.uid;
}
}
}
Error trace from terminal
FirebaseError: 7 PERMISSION_DENIED:
false for 'create' # L10
● Posts Rules › should not allow delete for normal user
FirebaseError: 7 PERMISSION_DENIED:
false for 'create' # L10
at new FirestoreError (/Users/../../../../../../../../../Resources/rules/node_modules/#firebase/firestore/src/util/error.ts:166:5)
at ClientDuplexStream.<anonymous> (/Users/../../../../../../../../../Resources/rules/node_modules/#firebase/firestore/src/platform_node/grpc_connection.ts:240:13)
at ClientDuplexStream._emitStatusIfDone (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client.js:234:12)
at ClientDuplexStream._receiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client.js:211:8)
at Object.onReceiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:1311:15)
at InterceptingListener._callNext (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (/Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:618:8)
at /Users/../../../../../../../../../Resources/rules/node_modules/grpc/src/client_interceptors.js:1127:18
I actually followed the same tutorial to get started with the firebase emulator and got the same kind of error messages. The problem for me was that when you start the simulator it automatically looks for your firestore.rules file and loads the rules. So, when you then add your mockData the rules already apply.
In order to make your test code work either change the setting for your firestore rules file in your firebase.json to a non-existing file (or rules file that allows all read/write) or add the mockData as an admin in your setup function, e.g.:
module.exports.setup = async (auth, data) => {
const projectId = `rules-spec-${Date.now()}`;
const app = firebase.initializeTestApp({
projectId,
auth
});
const db = app.firestore();
// Initialize admin app
const adminApp = firebase.initializeAdminApp({
projectId
});
const adminDB = adminApp.firestore();
// Write mock documents before rules using adminApp
if (data) {
for (const key in data) {
const ref = adminDB.doc(key);
await ref.set(data[key]);
}
}
// Apply rules
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync('firestore.rules', 'utf8')
});
return db;
};
Hope this helps.
Also see this question
For those that are currently having this issue firestore 8.6.1 (or equivalent), there is a bug discussed here:
https://github.com/firebase/firebase-tools/issues/3258#issuecomment-814402977
The fix is to downgrade to firestore 8.3.1, or if you are reading this in the future and firestore >= 9.9.0 has been released, upgrade to that version.