# Pengine outputs just object object

**URL:** https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532
**Category:** Help!
**Created:** [October 8, 2021, 9:41am UTC](https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532 "2021-10-08T09:41:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jrdn40](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jrdn40](https://swi-prolog.discourse.group/u/Jrdn40)
#### Post date: [October 8, 2021, 9:41am UTC](https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532/1 "2021-10-08T09:41:43Z")

</div>

Hey its me again, started working with pengines and overall it works. Now i tried to put my simple prolog code into pengine and changed those writes and reads with the equals of pengine, but i just get “Object object” as output or if i use stringify just [()]. Where my problem?

```prolog
<script type="text/x-prolog">
car(vw,'SUV',tiguan,2010,benzin).

read_car:-
pengine_input("Which car brand do you prefer?", Carbrand),
pengine_input("Which type do you prefer?(SUV,Coupe)",Type),
pengine_input("Which fuel do you prefer?",Fuel),
car(Carbrand,Type,Output1,Output2,Fuel),
pengine_output(Output1),
pengine_output(Output2).

</script>

<script type="text/javascript">
/*
Create a Prolog engine running the code from the above script and the
query specified in `ask`. Get the results in chunks of max 1,000 entries
and ask for more results if there are more available.
*/

 var pengine = new Pengine({ server: "https://swish.swi-prolog.org/pengine",
		ask: "read_car",
		application: "swish",
        onprompt: handlePrompt,
        onsuccess: handleOutput,
        onerror: handleOutput
            });
            function handlePrompt() {
                pengine.respond(prompt(this.data));
		   }
            function handleOutput() {
			var solution = JSON.stringify(this.data);

              $('#out').append(this.data + "<br/>");
		   }
</script>

<div id="out"></div>

```

also another question how can i implement basic if then logic? for example if the user choose “Tesla” as carbrand, i dont want prolog to ask for the type of fuel, because obviously tesla is just electro.

Thank you again guys!

---

<div class="post-metadata">

### Author: ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)
#### Post date: [October 8, 2021, 10:14am UTC](https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532/2 "2021-10-08T10:14:45Z")

</div>

> [@Jrdn40](#):
>
> how can i implement basic if then logic

Use conditional -\>/2.

In imperative pseudo code

```plaintext
If <condition> then
  <true action>
else
  <false action>

```

In Prolog using conditional

```prolog
(
   <condition>
->
   <true action>
;
  <false action>
)

```

If you use -\>/2 I would strongly suggest that both actions always have something, true/0 can be used and is often used, e.g.

```prolog
(
   <condition>
->
   <true action>
;
   true
)

```

That says that if the condition succeeds then do the `<true action>` otherwise when the condition fails do the `<false action>` which is `true` and just succeeds doing nothing.

* * *

A very common mistake made with the condition is to use `=` (unification) thinking it is a comparison; in Prolog `==` is a comparison. See: [Comparison and Unification of Terms](https://www.swi-prolog.org/pldoc/man?section=compare)

This is typically bad in a condition,

```prolog
A = 1

```

This is probably what you want

```prolog
A == 1

```

* * *

If you know your code is deterministic (has only one possible answer) then use det/1, e.g.

```prolog
:- det(<predicate indicator>). 

```

or use [single sided unification](https://www.swi-prolog.org/pldoc/man?section=ssu) =\>/2.

* * *

Also pay very close attention to the characters used for the operators  
`->` is for conditional  
`=>` is for single sided unification.

* * *

As far the pengine questions I can not help.

---

<div class="post-metadata">

### Author: ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)
#### Post date: [October 8, 2021, 9:27pm UTC](https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532/3 "2021-10-08T21:27:47Z")

</div>

Here is a small sample that shows you how to use pengine\_input/2 and pengine\_output/1.

It is a slight modification of [packages-pengines/input\_output.html at 0ab89f53ed1698e332e88860a396b3bc1350d677 · SWI-Prolog/packages-pengines · GitHub](https://github.com/SWI-Prolog/packages-pengines/blob/0ab89f53ed1698e332e88860a396b3bc1350d677/examples/web/input_output.html) to use SWISH.

```prolog
<html lang="en">
    <head>
        <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="https://swish.swi-prolog.org/pengine/pengines.js"></script>
        <script type="text/x-prolog">

	   read_names :-
        pengine_input('Please enter a name, stop with "stop"', X),
		( X == stop
		-> !
		; pengine_output(X),
		   read_names
		).

        </script>
        <script>
	   var pengine = new Pengine({
   	            server: "https://swish.swi-prolog.org/pengine",
   	            chunk: 1000,
   	            application: "swish",
                oncreate: handleCreate,
                onprompt: handlePrompt,
                onoutput: handleOutput
            });
            function handleCreate() {
                pengine.ask('read_names');
	   }
            function handlePrompt() {
                pengine.respond(prompt(this.data));
	   }
            function handleOutput() {
                $('#out').append("<div>"+this.data+"</div>");
	   }
        </script>
    </head>
    <body>
        <div id="out"></div>
    </body>
</html>

```

---

<div class="post-metadata">

### Author: ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)
#### Post date: [October 8, 2021, 9:46pm UTC](https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532/4 "2021-10-08T21:46:27Z")

</div>

Sorry, didn’t notice your actual problem before, either:

1. You are entring data that doesn’t match your car/5 predicate.  
or
2. You have invalid prolog code (usually a syntax error).

Use the following code and it should work:

```prolog
 car(vw,'SUV',tiguan,2010,benzin).
 read_car :-
	 pengine_input("Which car brand do you prefer?", Carbrand),
	 pengine_input("Which type do you prefer?(SUV,Coupe)",Type),
	 pengine_input("Which fuel do you prefer?",Fuel),
	 ( car(Carbrand,Type,Output1,Output2,Fuel)
	 -> pengine_output(Output1),
		pengine_output(Output2)
	 ; pengine_output("Unknown car")
	 ).

```

EDIT: I suggest you write and test your prolog code in the local machine first before you put it in your html file.

---

<div class="post-metadata">

### Author: ![Jrdn40](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jrdn40](https://swi-prolog.discourse.group/u/Jrdn40)
#### Post date: [October 9, 2021, 9:17am UTC](https://swi-prolog.discourse.group/t/pengine-outputs-just-object-object/4532/5 "2021-10-09T09:17:37Z")

</div>

> [@swi](#):
>
> ```prolog
> car(vw,'SUV',tiguan,2010,benzin).
> read_car :-
> pengine_input("Which car brand do you prefer?", Carbrand),
> pengine_input("Which type do you prefer?(SUV,Coupe)",Type),
> pengine_input("Which fuel do you prefer?",Fuel),
> ( car(Carbrand,Type,Output1,Output2,Fuel)
> -> pengine_output(Output1),
> pengine_output(Output2)
> ; pengine_output("Unknown car")
> ).
> 
> ```

Thanks, i used “onsuccess” and it still gave me [Object object]. I changed it to onoutput and now it works. Dont know why it dont work with onsuccess.
