Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...