Rank 2: Process
function returned empty response even though user account created successfully. Need user id after creating the new user account.
Function code:
import 'dart:convert';
import 'package:dart_appwrite/dart_appwrite.dart';
import 'package:dart_appwrite/models.dart' as models;
Future main(final context) async {
try {
final client = Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('PROJECT_ID')
.setKey('API_KEY')
.setSelfSigned(status: true);
final users = Users(client);
final reqBody = context.req.body;
final Map<String, dynamic> jsonMap = reqBody is String
? jsonDecode(reqBody)
: reqBody as Map<String, dynamic>;
final String? email = jsonMap['email'];
final String? passw = jsonMap['passw'];
final String? phone = jsonMap['phone'];
final String? name = jsonMap['name'];
if (email == null || passw == null) {
return context.res.json({
"message": "Missing email or password",
"type": "validation_error",
"code": 400
}, status: 400);
}
// Create new user
models.User newUser = await users.create(
userId: ID.unique(),
email: email,
password: passw,
phone: phone,
name: name ?? '',
);
// Return success response with userid
return context.res.json({
"message": newUser.$id, // userid
"type": "success",
"code": 201
}, status: 201);
} on AppwriteException catch (e) {
return context.res.json({
"message": e.message ?? 'Appwrite error',
"type": e.type ?? 'appwrite_error',
"code": e.code ?? 520
}, status: e.code ?? 520);
} catch (e) {
return context.res.json({
"message": e.toString(),
"type": "general_error",
"code": 520
}, status: 520);
}
}