dbrwk
dbrwk2mo ago

Cant update user value in Firestore

Hi, i want to update firestore field via node. Try 1 as a new value i pass data.user_id, but in console i see in the Input section: "_ref:[object Object]" Try 2 i stringify it just to make sure, that i have access to this data, i see it, and when i pass to firestore stringyfy node output - i got an error. Try 3 (no screenshot) If i hardcode user ref - document is updated correctly so it is not firestore rules related. Any ideas? Oh, i tinkered in node code, so maybe i messed up something...
No description
No description
6 Replies
AI Support
AI Support2mo ago
It sounds like there might be an issue with how the data is being passed to the Firestore node. Since you mentioned tinkering with the node code, it could be beneficial to reset the node to its default state and try again. Here are a few steps you can take to troubleshoot: 1. Reset the Node: Delete the current Firestore node and add a new one to ensure it's in its default state. 2. Check Data Passing: Ensure that the data you are passing to the Firestore node is in the correct format. Firestore nodes typically expect a specific structure for document references and data. For detailed guidance on using Firestore nodes, you can refer to the BuildShip documentation on Firestore nodes: Firestore Node Documentation. If the issue persists, please submit a support request via the in-app support button for further assistance.
dbrwk
dbrwk2mo ago
import { Firestore, DocumentReference } from '@google-cloud/firestore';

function createNestedObject(path, value) {
let obj = value;
for (let i = path.length - 1; i >= 0; i--) {
obj = { [path[i]]: obj };
}
return obj;
}

function convertDataToReferences(firestore: Firestore, data: Record<string, any>): Record<string, any> {
const convertedData: Record<string, any> = {};
for (const key in data) {
const value = data[key];
// _ref:collectionName/documentId
if (typeof value === 'string' && value.includes('/') && value.startsWith('_ref:')) {
const path = value.split('_ref:')[1];
try {
const ref = firestore.doc(path);
if (ref instanceof DocumentReference) {
convertedData[key] = ref;
} else {
convertedData[key] = value;
}
} catch (error) {
convertedData[key] = value;
}
} else if (typeof value === 'object' && value !== null) {
convertedData[key] = convertDataToReferences(firestore, value);
} else {
convertedData[key] = value;
}
}
return convertedData;
}

export default async function updateFirestoreFieldValue({
docPath,
fieldKey,
newValue,
projectId
}: NodeInputs, { logging }: NodeScriptOptions): NodeOutput {
const firestore = new Firestore(
projectId && projectId.length > 1 ? { projectId: projectId.trim() } : undefined
);

const docRef = firestore.doc(docPath);

// Convert the newValue to handle document references
const convertedNewValue = convertDataToReferences(firestore, { value: newValue }).value;

const fieldKeys = fieldKey.split('.');
const dataObject = createNestedObject(fieldKeys, convertedNewValue);

const result = await docRef.set(dataObject, { merge: true });
return result;
}
import { Firestore, DocumentReference } from '@google-cloud/firestore';

function createNestedObject(path, value) {
let obj = value;
for (let i = path.length - 1; i >= 0; i--) {
obj = { [path[i]]: obj };
}
return obj;
}

function convertDataToReferences(firestore: Firestore, data: Record<string, any>): Record<string, any> {
const convertedData: Record<string, any> = {};
for (const key in data) {
const value = data[key];
// _ref:collectionName/documentId
if (typeof value === 'string' && value.includes('/') && value.startsWith('_ref:')) {
const path = value.split('_ref:')[1];
try {
const ref = firestore.doc(path);
if (ref instanceof DocumentReference) {
convertedData[key] = ref;
} else {
convertedData[key] = value;
}
} catch (error) {
convertedData[key] = value;
}
} else if (typeof value === 'object' && value !== null) {
convertedData[key] = convertDataToReferences(firestore, value);
} else {
convertedData[key] = value;
}
}
return convertedData;
}

export default async function updateFirestoreFieldValue({
docPath,
fieldKey,
newValue,
projectId
}: NodeInputs, { logging }: NodeScriptOptions): NodeOutput {
const firestore = new Firestore(
projectId && projectId.length > 1 ? { projectId: projectId.trim() } : undefined
);

const docRef = firestore.doc(docPath);

// Convert the newValue to handle document references
const convertedNewValue = convertDataToReferences(firestore, { value: newValue }).value;

const fieldKeys = fieldKey.split('.');
const dataObject = createNestedObject(fieldKeys, convertedNewValue);

const result = await docRef.set(dataObject, { merge: true });
return result;
}
AI Support
AI Support2mo ago
For detailed guidance on how to interact with Firestore using BuildShip, you can refer to the following documentation links: - Firestore CRUD with no code - visual workflow builder - How to connect BuildShip to your Firebase Project - Firestore CRUD Operations If you need further assistance, please submit your query via the in-app support button.
BuildShip
YouTube
Firestore CRUD with no code - visual workflow builder
Get started at https://buildship.com This video shows how you can use BuildShip's built in Firestore. If you want to use your own Firestore then simply follow these short steps to connect to your Firebase https://docs.buildship.com/tutorials/firebase-project Then you can build workflows with Firestore CRUD with ease no code style. 💬 Chat: htt...
Firestore Database – BuildShip
A unified resource to start building your backend with low-code. Dive into triggers, nodes, and step-by-step guidance to jumpstart your workflow creation.
How to connect BuildShip to your Firebase Project – BuildShip
A unified resource to start building your backend with low-code. Dive into triggers, nodes, and step-by-step guidance to jumpstart your workflow creation.
AI Support
AI Support2mo ago
This solution is marked as answered ✅
dbrwk
dbrwk2mo ago
actually, my AI Chatbot friend, resetting the node fixed the problem. And it turned out, i didnt tinker with this code (did a diffcheck to be sure) so it was some kind of a bug.
AI Support
AI Support2mo ago
It sounds like resetting the node resolved the issue you were facing. If you encounter any further problems or if this issue persists, you might want to report it via the in-app support button to ensure it gets investigated as a potential bug. For more information, you can refer to the BuildShip documentation on nodes and troubleshooting: BuildShip Documentation. If you need additional assistance, please submit your query via the in-app support button.