node js function return [object Object] instead of a string value

后端 未结 3 985
孤城傲影
孤城傲影 2021-02-05 20:43

i am quiet new to java script and node js, i am trying to get a value from a MySQL DB, and the return value is [object Object] instead of a string. i didn\'t really found any a

3条回答
  •  情歌与酒
    2021-02-05 21:13

    I also encountered this issue, running the following code in a node.js terminal in conjunction with "watchman-make" (watchman-make: see comments in first answer at https://www.quora.com/What-IDEs-are-available-for-node-js-development-on-Linux).

    The following code (with node.js output shown) illustrates the points made in the accepted answer/comments:

    function arrayToList(array) {
      var list = {};
      for (var i = array.length - 1; i >= 0; i--) {
        list = {value: array[i], rest: list};
      }
      return list;
    };
    
    console.log(arrayToList( [1, 2, 3, 4, 5] ));
    // { value: 1,
    //  rest: { value: 2, rest: { value: 3, rest: [Object] } } }
    
    // '[Object]' ?
    // http://stackoverflow.com/questions/34264800/node-js-function-return-object-object-instead-of-a-string-value
    
    var obj = arrayToList([1, 2, 3, 4, 5]);
    
    console.log('%j', obj);
    // {"value":1,"rest":{"value":2,"rest":{"value":3,"rest":{"value":4,"rest":{"value":5,"rest":null}}}}}
    
    console.log(JSON.stringify(obj));
    // {"value":1,"rest":{"value":2,"rest":{"value":3,"rest":{"value":4,"rest":{"value":5,"rest":null}}}}}
    

提交回复
热议问题