Reputation: 1206
I'm trying to copy my real time database contents to firestore. But I'm getting this error when I use the set function.
exports.copyPosts = functions.https.onRequest((req, res) => {
var i = 0;
var username;
db.ref("feeds").child("all").limitToLast(2000).once("value", function (postSnap) {
console.log(postSnap.numChildren());
postSnap.forEach(function(topic){
i = i + 1;
console.log(topic.key);
firestore.collection("topics").doc("all").collection(i+"").set({
caption: topic.child("caption").val(),
time: topic.child("time").val(),
username: topic.child("username").val(),
category: topic.child("category").val(),
pic: topic.child("pic").val()
},function(error) {
if (error) {
alert("Data could not be saved." + error);
} else {
alert("Data saved successfully.");
}
});
if(postSnap.numChildren()==i){
res.contentType('application/json');
res.status(200).send("Success");
}
});
});
});
Error Log
Error: Argument "options" is not a valid SetOptions. Input is not an object. at exports.(anonymous function) (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:86:15) at Object.exports.(anonymous function) [as isOptionalSetOptions] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:91:35) at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:268:14) at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:425:8) at /user_code/index.js:2172:47 at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/api/DataSnapshot.js:126:20 at LLRBNode.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/SortedMap.js:170:13) at LLRBNode.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/SortedMap.js:169:27) at LLRBNode.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/SortedMap.js:169:27) at LLRBNode.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/SortedMap.js:169:27)
Upvotes: 3
Views: 1073
Reputation: 317760
The API documentation for the set() method states that it takes two arguments:
You're passing it two arguments, an object, and a function. It appears that you expect the function to be called in the event of an error, but that's not how the documentation is saying that it works. The error you're getting the API seeing that you're not passing a valid options object at the optional second parameter.
If you're interested in the result of the set() operation, pay attention to the promise that it returns. The convention is that a promise will be rejected in the case of an error. If you're going to be writing Cloud Functions code, you definitely need to be familiar with how promises work.
Upvotes: 1