#[SOLVED] the cloud document creation doesn't register my relationships inside it

30 messages · Page 1 of 1 (latest)

grizzled charm
#

My relationships Doesn't work in the cloud function it doesn't register any relationship inside it, even before i send the data the orderRequest i look inside it everything is available but the data of the relation when it creates the orderResult becomes and empty array.

this is my cloud function code:
try {
const itemsQuery = await database.listDocuments(
"654b2f6a8af9b2ed391f",
"654b2f8078d73f2fae55",
[
Query.equal("$id", items.map(item => item.itemId)),
Query.limit(items.length),
Query.isNull("deletedAt"),
],
);

  log(itemsQuery);
  const itemsData = itemsQuery.documents;

  const totalAmount = itemsData.reduce((acc, item) => {
    const itemData = items.find(i => i.itemId === item.$id);
    return acc + itemData.quantity * item.price;
  }, 0);

  const orderRequest = {
    userId: userId,
    status: 0, // Assuming 0 is the initial status
    totalAmount: totalAmount,
    items: itemsData,
  };

  log("Order Request");
  log(orderRequest);

  const orderResult = await database.createDocument("654b2f6a8af9b2ed391f", "6564217c78122dc22f6f", ID.unique(), orderRequest);
  log("Order Result");
  log(orderResult);

  try{
    for (const [index,item] of itemsData.entries()) {
      const itemsBlockerRequest ={
        orders: orderResult,
        items: item,
        quantity: items[index].quantity,
      }

      log("Items Blocker Request");
      log(itemsBlockerRequest);

      await database.createDocument("654b2f6a8af9b2ed391f", "65670d502687c6a34e76", item.$id, itemsBlockerRequest);
    }
  }catch(e){
    log('Something went wrong: '+e);
    return res.send('Something went wrong');
  }
  return res.send("Order created successfully");
} catch (e) {
  log('Something went wrong: '+e);
  return res.send('Something went wrong');
}
fringe mason
grizzled charm
# fringe mason Items should just be an array of items IDs.

yeah that fixed that but i have another problem, the order i create first time in the cloud function get created perfectly but after that, the ID.unique doesn't change and rewrite the whole order object.
this is my code btw:

        status: 0, // Assuming 0 is the initial status
        totalAmount: totalAmount,
        userId: userId,
        items: items.map(item => item.itemId),
        address: addressId
      };

      log("Items Data Before "+ID.unique());
      log("After "+unique());
      log("Order Request");
      log(orderRequest);
      const orderResult = await database.createDocument("654b2f6a8af9b2ed391f", "6564217c78122dc22f6f", ID.unique(), orderRequest);
      log("Order Result");
      log(orderResult);
        items.forEach(async (item,index) => {
          const itemsBlockerRequest ={
            orders: orderResult.$id,
            items: item.itemId,
            quantity: items[index].quantity,
          }

          log("Items Blocker Request "+index);
          log(itemsBlockerRequest);
  
        await database.createDocument("654b2f6a8af9b2ed391f", "65670d502687c6a34e76", ID.unique(), itemsBlockerRequest);
          log("Items Blocker Result ");
        });
      return res.send("Order created successfully");
#

and the error:
Something went wrong: Error: Document with the requested ID already exists. Try again with a different ID or use ID.unique() to generate a unique ID.

fringe mason
# grizzled charm yeah that fixed that but i have another problem, the order i create first time i...

Are you saying:

const orderResult = await database.createDocument("654b2f6a8af9b2ed391f", "6564217c78122dc22f6f", ID.unique(), orderRequest);

Is throwing this error?

Something went wrong: Error: Document with the requested ID already exists. Try again with a different ID or use ID.unique() to generate a unique ID.

If so, do you have a unqiue index on the orders collection? or the item collection?

grizzled charm
#

Yeah i just found out the problem is with the relationships if they have the same items in them it will not create another order it will replace the order that has been created, can u suggest me anyways to fix this?

fringe mason
grizzled charm
#

Should i send a picture of the collection attributes?

fringe mason
fringe mason
grizzled charm
grizzled charm
fringe mason
#

I just tried to do something like:

const orderResult = await database.createDocument("654b2f6a8af9b2ed391f", "6564217c78122dc22f6f", ID.unique(), orderRequest);

and it worked fine...

What API call exactly is failing?

grizzled charm
#

Does it have a relation in it?

#
    try {
      const result = await Appwrite.functions.createExecution(
        "6566e3017ed5ff75b429",
        JSON.stringify(order),
        false,
        "/",
        "POST"
      );

      console.log("Executed");
      console.log(result);
    } catch (e) {
      console.log(e);
      throw e;
    }
  }
#

i'm calling this function

#
    id?: string | null;
    userId: string;
    items: RequestSelectedItems[];
}

interface RequestSelectedItems{
    itemId: string;
    quantity: number;
}```
#

this is the model of order i'm sending

grizzled charm
#

example like this i just created an order with two items inside it, then created another order with the same two item inside it it just stole the items inside it from the previoues order.

fringe mason
#

soo i think it's because it's a one to many relationship. Regardless of it being only 1 way, an item can only be linked to 1 order. You may need to use a many to many relationship

grizzled charm
#

Yesss thank you so much, it fixed it . But is this a problem with appwrite or just how sql works?

fringe mason
grizzled charm
#

Thank you so much

abstract rapids
#

[SOLVED] the cloud document creation doesn't register my relationships inside it

meager glade
#

internal server when i am trying to create a new document async addProduct(prodData) {
try {
return await this.databases.createDocument(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
ID.unique(),
prodData
);
} catch (error) {
console.log("databaeService::addproduct::error: " + error);
}
}