Hi ,
I keep getting “databases.upsertDocuments” or “createDocuments” function is not a valid appwrite function error . Following is a simple node.js code (modified for bug reporting purpose)
Could you please investigate and let me know the root cause ? Thanks .
Execution id : 687e990ea3b4f735382a
import { Client, Databases, ID } from 'node-appwrite';
export default async ({ req, res, log, error }) => {
let payload;
try {
payload = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
} catch (err) {
return res.json({ success: false, error: 'Invalid JSON', data: null }, 400);
}
const { ast_id, room_id, type, score } = payload || {};
if (!ast_id || !room_id || !type || !score ) {
return res.json(
{ success: false, error: 'Missing required parameters: ast_id, room_id, type, score, data: null },
400
);
}
log(submit-prediction: ${JSON.stringify({ ast_id, room_id, type })});
const client = new Client()
.setEndpoint(process.env.APPWRITE_ENDPOINT)
.setProject(
process.env.APPWRITE_FUNCTION_PROJECT_ID ||
process.env.APPWRITE_PROJECT_ID
)
.setKey(req.headers['x-appwrite-key'] ?? '');
const databases = new Databases(client);
const dbId = process.env.APPWRITE_DATABASE_ID;
try {
log(`Using score from payload: ${score}`);
const currentDate = new Date().toISOString().split('T')[0];
const documentId = `pred_${ast_id}_${room_id}_${type}_${currentDate}`
.replace(/[^a-zA-Z0-9_-]/g, '_')
const predictionDoc = await databases.upsertDocuments(
dbId,
'predictions',
[{
$id: documentId,
ast_id,
room_id,
type,
score
}]
);