Вопрос

I'm working on a calorie counter and I can create new recipes, but I am having problems trying to update an ingredient.

I can create an ingredient with the following mql:

{
  id: recipeId,
  '/food/recipe/ingredients': {
    create: 'unless_exists',
    id: null,
    quantity: parseFloat( row.$quantity.val() ),
    unit: {
      id: row.$units.val()
    },
    ingredient: {
      id: row.ingredientId
    }
  }
}

However if I change an ingredient's measure, say from 2 to 3 cups, this query will create a new ingredient. I've been trying to update the entry. So far I've tried:

{
  connect: 'update',
  id: row.rowId,
  type: '/food/recipe_ingredient',
  quantity: parseFloat( row.$quantity.val() ),
  unit: {
    id: row.$units.val()
  },
  ingredient: {
    id: row.ingredientId
  }
}

This results in the error: Can't use [], [{}] or {} in a write.

{
  connect: 'update',
  id: row.rowId,
  type: '/food/recipe_ingredient',
  quantity: parseFloat( row.$quantity.val() )
}

Results in the error: Can't use 'connect' at the root of the query.

 {
   id: recipeId,
   '/food/recipe/ingredients': {
     connect: 'update',
     id: row.rowId,
     type: '/food/recipe_ingredient',
     quantity: parseFloat( row.$quantity.val() )
   }
 }

I get the error: Can't use 'connect': 'update' on a non-unique master property.

{
  id: row.rowId,
  type: '/food/recipe_ingredient',
  quantity: {
    connect: 'update',
    value: parseFloat( row.$quantity.val() )                                                                                               
  }
}

Results in the error: This value is already in use. Please delete it first. Is the only way to delete the old ingredient and add a new one?

If the only way is to delete the old entries and replace them, is there any way to remove all the ingredients at once rather than individually with queries like:

{
  id: recipeId,
  '/food/recipe/ingredients': {
    connect: 'delete',
    id: row.rowId
  }
}
Это было полезно?

Решение

It's not a "subobject" but a separate node in its own right. I'd suggest reading about CVTs or mediator types. To change its properties you need to move the connect to the inner query.

Here's the yeast ingredient node from my pizza dough recipe

This query will update the quantity for the yeast (assuming there's only one ingredient node specifying yeast):

[{
  "id": "/m/0wh8nkh",
  "/food/recipe/ingredients": [{
    "ingredient": {
      "id": "/en/yeast"
    },
    "quantity": {
      "connect": "update",
      "value": 2
    },
    "unit": {
      "id": "/en/teaspoon",
      "connect": "update"
    }
  }]
}]

but rather than counting on ingredients being unique, I'd suggest fetching and saving the IDs of the ingredient nodes so that you can refer to them explicitly in your query.

Some other notes: - you shouldn't be creating new Dishes for every Recipe. Many dishes should already exist in Freebase and you should let the user select them separately from the recipe. - You need to add /common/topic to Dishes & Recipes that you create (but not to the CVTs for the ingredients)

It's great that you're using the sandbox so that you're not messing up the production database while you debug.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top