This is my code at android studio
val myDoc = Document().apply {
append("name", "futureTech")
append("video", "youtube")
append("startdate", "2003")
}
findViewById<Button>(R.id.button).setOnClickListener {
lifecycleScope.launch {
try {
val result = user.functions.call<String>("insertFunction", myDoc)
Log.i("TAG", "Function result: $result")
} catch (e: Exception) {
Log.e("TAG", "Error calling function: ${e.message}", e)
}
}
}`
And this is the “insertFunction” code
exports = async function(doc) {
if (typeof doc !== 'object' || doc === null) {
throw new Error("Invalid argument: Argument must be an object.");
}
const collection = context.services.get("mongodb-atlas").db("projectTest").collection("insertUser");
try {
const result = await collection.insertOne(doc);
return result;
} catch (e) {
console.error("Error inserting document:", e);
throw new Error("Failed to insert document.");
}
};
I want to add a piece of data to my database, but the error keeps showing up.
Error calling function: [Service][FunctionExecutionError(4313)] {"message":"Failed to insert document.","name":"Error"}. io.realm.kotlin.mongodb.exceptions.FunctionExecutionException: [Service] [FunctionExecutionError(4313)] {"message":"Failed to insert document.","name":"Error"}.
I’d like to ask for help. Is there any way to solve it?
I had change my data describe into
val myDoc = mapOf(
"name" to "futureTech",
"video" to "youtube",
"startdate" to "2003"
)
but it didn’t work
蔡宜樺 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.