FirebaseError: Function setDoc() called with invalid data. Unsupported field value: undefined - javascript

OrderDetails Logged image
I want to save order details to my firestore db,Here my orderDetails:
const orderDetails=[{
id:"ID1",
name:"foo"
},
{
id:"ID2",
name:"foo-foo"
},
]
Here is the code for adding this order Details to fire store:
.then(({paymentIntent})=>{
if(user){
console.log(basket);
const userRef=doc(db,'shopDB',user.uid);
const userOrderRef=doc(userRef,'userOrderInfo',paymentIntent.id);
setDoc(userOrderRef,{
orders: basket,
created :paymentIntent.created,
amount :paymentIntent.amount
},{merge:true})
Here all working if i set document without orderDetails,Other two values will added to fire store,but when i try to add orders to database it throws error
error>>>>>>>>>>>
FirebaseError: Function setDoc()
called with invalid data. Unsupported
field value: undefined
Can anyone help me with this?

looks like 1 of the 2 missing:
orderDetails is undefined
userOrderRef is undefined
try printing them both to make sure what's missing there
Edit:
now that I can see your console.log, your issue is with field alternative:undefined at element in index 1 according to your photo.
remove this property before trying to save it on firestore

Related

How can I use DEFAULT values via knex insert?

My goal is to dynamically insert data into a table via knex.
Code looks like this:
const knexService = require("../knexService.js")
async function insertObjectToKnex() {
const insertObject = {
id: "DEFAULT",
someKey: "someValue"
};
await knexService.db("table").insert(inserObject);
}
On DEFAULT the next free id should be used as database id - table is configured and it works with raw sql. With knex.js I get the following error:
invalid input syntax for type integer: "DEFAULT"
Using the useNullAsDefault: true, config is not possible, because the id is not nullable.
How can I trigger the default value in knex - I did not find anything in the documentation or with google, that could at least give a hint to this issue!
While it is not mentioned in the documentation of knex.js one should simply not add fields with a DEFAULT assignement to a query. This will set the default value to the row column.

Can only access some javascript object keys

I am noticing strange behavior from a javascript object in my Node application. The user object is created by parsing a csv file with two columns: Email and ID. For some reason, I can only access one of the fields using the typical methods. The other field always returns undefined. I am using Node 15.12.0.
When I log the user object, it returns: { 'Email': 'email#example.com', ID: '12345' }
Curiously, the ID field does not have quotes when it is logged, but the Email field does have quotes.
I can access user.ID as usual, but user.Email returns undefined.
console.log(Object.keys(user)); // [ 'Email', 'ID' ]
console.log(user.ID); // 12345
console.log(user.Email); // undefined
console.log(user['Email']) // undefined
console.log(user["Email"]) // undefined
console.log(user.email) // undefined
console.log(user["\'Email\'"]) // undefined
console.log(user["'Email'"]) // undefined
console.log(user[Object.keys(user)[0]]) // email#example.com
console.log(Object.values(user)[0]) // email#example.com
I have tried the using JSON.parse(JSON.stringify(user)), but I get the same results as before.
The only way I can access the Email field is by using the user[Object.keys(user)[0]] or Object.values(user)[0] which is very strange to me.
I would appreciate any ideas you have to figure out whats happening here. Is there something I can do to examine the object more closely? Does the quotes around the keys indicate anything?

Querying data in firestore by timestamp

I'm querying documents by timestamp and it is returning an empty array. However it works when I use "==" ex:.where('date', '==',timestamp), and returns empty array when I use '>=' or '<='.
I have tried to convert timestamp into date object and string also but no success.
Note: The date field in firestore is of type Timestamp.
I'm querying documents with date greater than '2018-08-03' in the collection.
Below is a picture of the collection of transactions (left) and the document(right) which should be part of the array of documents returned, because the date is greater than '2018-08-03'
Below is my code.
const firstDay = new Date('2018-08-03');
const timestamp1 = admin.firestore.Timestamp.fromDate(firstDay);
const trans = [];
const docRef = db.collection('Users').doc(uid).collection('transactions').where('item_id', '==', item_id)
.where('date', '>=', timestamp1);
await docRef.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
trans.push({ transaction_id: doc.id, transaction: doc.data() });
});
})
.catch(err => new Error('cannot get the documents', err));
Expected result: should be an array with transactions with date greater than specified above.
Actual result: empty array.
Since its working for equality ==, I assumed that >= and <= would work. Anything I'm missing here?
Logs after trying ">" (equal to timestamp1)
Firestore (2.3.0) 2019-10-03T14:01:25.013Z AObRG [ClientPool.acquire]: Re-using existing client with 100 remaining operations
Firestore (2.3.0) 2019-10-03T14:01:25.015Z AObRG [Firestore.readStream]: Sending request: {"parent":"projects/valuemo-000/databases/(default)/documents/Users/Xmr3vKT19OSST02DSTDMt0jSq692","structuredQuery":{"from":[{"collectionId":"transactions"}],"where":{"compositeFilter":{"op":"AND","filters":[{"fieldFilter":{"field":{"fieldPath":"item_id"},"op":"EQUAL","value":{"stringValue":"zGRkpP3QkzI89zKyDjZ7FPDrXd5G3Bco5ENlR"}}},{"fieldFilter":{"field":{"fieldPath":"date"},"op":"EQUAL","value":{"timestampValue":{"seconds":1533254400}}}}]}}}}
Firestore (2.3.0) 2019-10-03T14:01:25.232Z AObRG [Firestore._initializeStream]: Received stream error: { Error: The query requires a COLLECTION_ASC index
for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB
at Http2CallStream.call.on (D:\Projects\Valuemo-firebase\node_modules\#grpc\grpc-js\build\src\call.js:68:41)
at Http2CallStream.emit (events.js:194:15)
at process.nextTick (D:\Projects\Valuemo-firebase\node_modules\#grpc\grpc-js\build\src\call-stream.js:71:22)
at process.internalTickCallback (internal/process/next_tick.js:70:11)
code: 9,
details:
'The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB',
metadata: Metadata { options: undefined, internalRepr: Map {} } }
Firestore (2.3.0) 2019-10-03T14:01:25.256Z AObRG [Firestore._initializeStream]: Received initial error: { Error: The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB
at Http2CallStream.call.on (D:\Projects\Valuemo-firebase\node_modules\#grpc\grpc-js\build\src\call.js:68:41)
at Http2CallStream.emit (events.js:194:15)
at process.nextTick (D:\Projects\Valuemo-firebase\node_modules\#grpc\grpc-js\build\src\call-stream.js:71:22)
at process.internalTickCallback (internal/process/next_tick.js:70:11)
code: 9,
details:
'The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB',
metadata: Metadata { options: undefined, internalRepr: Map {} } }
Firestore (2.3.0) 2019-10-03T14:01:25.260Z AObRG [Firestore._retry]: Request failed with unrecoverable error: { Error: The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB
at Http2CallStream.call.on (D:\Projects\Valuemo-firebase\node_modules\#grpc\grpc-js\build\src\call.js:68:41)
at Http2CallStream.emit (events.js:194:15)
at process.nextTick (D:\Projects\Valuemo-firebase\node_modules\#grpc\grpc-js\build\src\call-stream.js:71:22)
at process.internalTickCallback (internal/process/next_tick.js:70:11)
code: 9,
details:
'The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet. See its status here: https://console.firebase.google.com/project/valuemo-000/database/firestore/indexes/single_field?create_exemption=ClJwcm9qZWN0cy92YWx1ZW1vLTAwMC9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvdHJhbnNhY3Rpb25zL2ZpZWxkcy9kYXRlEAEaCAoEZGF0ZRAB',
metadata: Metadata { options: undefined, internalRepr: Map {} } }
So I was testing it out, with what I believe to be a very similar set up to what you presented, and I was actually able to reproduce the same behaviour.
After testing different possibilities, I found that the issue for me had nothing to do with the datatype of the fields, but with the configuration of the composite index.
The composite index configuration required to make this query work was the following:
According to the Queries supported by composite indexes section of the documentation, the indexes for these kind of compound queries should have the equality filtered field first, this is not explicitly said, but it is how the docs present it in the examples.
Let me know if this managed to resolve the issue for you.
Take your date and convert into javascript date object. Then do getTime() on converted date object it will return a number. Then get firestore timestamp and do getSeconds().Compare seconds value of firestore timestamp with the variable storing output of getTime().
The query requires a COLLECTION_ASC index for collection transactions and field date. That index is not ready yet.
Why not create the index as the error is suggesting? This might fundamentally solve your issue.

Algolia Instant Search Firebase Cloud Function - how to get the other value?

I don't have much idea about JavaScript, so I used Algolia's Instant Search for Firebase Github Repository to build my own function.
My function:
exports.indexentry = functions.database.ref('/posts/{postid}/text').onWrite(event => {
const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
const firebaseObject = {
text: event.data.val(),
timestamp: event.data.val(),
objectID: event.params.postid
};
In Algolia indices, with timestamp as the key, I get the same value as in text field, but in Firebase backend timestamp is different. How to fix this?
I tried different statements to get timestamp value but couldn't.
Edit
Expected Outcome:
{
text: "random rext",
timestamp: "time stamp string",
author: "author name",
object ID: "object ID"
}
Actual Outcome
{
text: "entered text",
object ID: "object ID"
}
I'm not real clear about your goal. Event has a timestamp property. Have you tried:
const firebaseObject = {
text: event.data.val(),
timestamp: event.timestamp, // <= CHANGED
objectID: event.params.postid
};
If you want a long instead of string, use Date.parse(event.timestamp)
EDIT 2: Answer can be found here.
Original Answer: What Bob Snyder said about the timestamp event is correct.
There may be other fields as well, for example, author_name that we may need to index, is there a generalized way to do that or do I write separate functions for every field?
If you want a general way to add all fields, I think what you are looking for can be found here. This should give you the right guidance to get what you want, i.e save your whole object into the Algolia index.
EDIT:
index.saveObject(firebaseObject, function(err, content) {
if (err) {
throw err;
}
console.log('Firebase object indexed in Algolia', firebaseObject.objectID);
});
event.data.val() returns the entire firebase snapshot. If you want a specific value in your data you add it after .val() for example if every post has an author stored in your firebase database under they key "author" you can get this value using var postAuthor = event.data.val().author
I've included some samples from my code for those interested. A sample post looks like this:
Then inside my cloud functions I can access data like this:
const postToCopy = event.data.val(); // entire post
const table = event.data.val().group;
const category = event.data.val().category;
const region = event.data.val().region;
const postKey = event.data.val().postID;

How to query Firebase data after using .push() to add data?

Here is the code for when I'm pushing the data to Firebase:
firebase.database().ref(`booklogs/${uid}/${book_id}`).push(page_id)
booklogs :
{HUMjSHxVKAPfVXzOId9zCBkGOgv1:{
book28917: {
-KYp4FdYYODDZG1FX-Pb: 1
}
}
}
My problem is when I query the data, the child node of the ${book_id} includes the push key, but I only want to get the value which is 1 and not the push key.
The code I use to query is:
var booklogs = db.ref(`booklogs/${uid}/${project}`);
booklogs.once('value')
.then((snapshot) => {
console.log(`pages viewed are ${snapshot.key}: ${snapshot.val()}`);
console.dir(snapshot.val());
}).catch((error) => {
console.log(`Error : ${error}`);
});
The data returned in the console is:
pages viewed are 2634651: [object Object]
{ '-KYp4FdYYODDZG1FX-Pb': 1 }
Any input would be much appreciated. Thanks!
If you only want the '1' and not the push key, try using .set()
firebase.database().ref(`booklogs/${uid}/${book_id}`).set(page_id)
That will get rid of the object and just give you the value that you wanted. Push automatically generates a key for every value you add, so you will always get an object back. From the Firebase docs - "For basic write operations, you can use set() to save data to a specified reference, replacing any existing data at that path."
https://firebase.google.com/docs/database/web/read-and-write

Categories

Resources