Console.log()记录我[object,object]的东西

Ahm*_*man -2 javascript json node.js steambot

好的你好,所以我有一个异步的东西并返回一个promise,基本上我已经使用了.then方法或其他任何方法.虽然当我在console.log()中我想要记录的变量+一个自定义字符串(console.log(data +"hey");)它给出了log [object,object]当我单独执行变量数据时,它给了我正是我想要的.为什么会这样?这是我的代码btw.

(此代码获取我在库存中的项目.):

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

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

这很完美(因为console.log中的数据是单独的)另外,当我尝试将"数据"放在这样的json文件中时:

        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!");
        }));
Run Code Online (Sandbox Code Playgroud)

它给了我同样好的旧[物体,物体] ......更多的线条

在json文件中.它没有给我我想要的东西.

(顺便说一句,console.log(数据)给了我):

 { 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:
Run Code Online (Sandbox Code Playgroud)

这是我想要存储在json文件中的内容.

谢谢你的时间.

Sov*_*iut 6

你的data变量不是字符串,它是一个对象.JSON.stringify()在将对象写入文件之前,需要使用它将对象转换为JSON字符串.否则,您只需编写对象的字符串表示形式即可[object Object].

在您的情况下,您可以在编写文件时进行转换:

fs.writeFile("inventories/me.json", JSON.stringify(data), function(err) {
Run Code Online (Sandbox Code Playgroud)