javascriptjsonnode.jssteambot

Console.log() logs me [object,object] something


Okay hello, so I have a something that is async and returns a promise, basically I've used the .then method or whatever it is called. Though when I console.log() the variable I want to log + a custom string (console.log(data+"hey");) it gives logs [object,object] when I do the variable alone only data, it gives me exactly what I want. Why is this happending? Here's my code btw.

(this code gets the items I have in my inventory on steam.):

var steamUserInventory = require('steam-user-inventory');

var botID64 = "76561198026027024";
steamUserInventory(botID64, '753/6').then(data => console.log(data));

This works perfect (because the data in console.log is alone) Also, when I try to put "data" in a json file like this:

        steamUserInventory(Bot, '753/6').then(data => 
        fs.writeFile("inventories/me.json", data, function(err) {
            if(err) {
                console.log("Error saving data to json file: " + err);
                return;
            }
            console.log("Bot inventory has been updated!");
        }));

It gives me the same good old [object, object]..many more lines

in the json file. It does not give me what I want.

(btw, the console.log(data) gives me):

 { id: '538277908',
amount: '1',
pos: 126,
name: 'Docking',
appid: '753',                                                                                                           classid: '230924010',
instanceid: '2108281766',                                                                                               tradable: 0,
marketable: 0,                                                                                                          marketTradableRestriction: '7',
link: 'http://cdn.akamai.steamstatic.com/steamcommunity/public/images/items/2870/5059522765a958a85cabe31988ccc928b8c36715.jpg',
image: 'http://steamcommunity-a.akamaihd.net/economy/image/U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIoxXpgK8bPeslY9pPJIvB5IWW2-452kaM8heLSRgleGBrrJJ3upnaKks0uKrDFhw5OJPBT3mTBHXgjSUcef3xwM2PZYmJ0fxw5kZ79tJA8F0arJkgNs',
category: null,                                                                                                         type: null,
exterior: null,                                                                                                         quality:

which is what I want to be stored in the json file.

Thanks for your time.


Solution

  • Your data variable is not a string, it's an object. You need to use JSON.stringify() to convert your object to a JSON string before writing it to a file. Otherwise, you'll just write the string representation of the object, which is [object Object].

    In your case, you can do the conversion when you write your file:

    fs.writeFile("inventories/me.json", JSON.stringify(data), function(err) {