[对象对象]可以't确定并显示在控制台中

The [object object] can't be determined and shown in console

本文关键字:对象 显示 控制台 可以      更新时间:2023-09-26

我有一个函数,它返回类似[object object]的东西,没有我想要的值,我几乎做了所有的事情来获得值,但没有希望。

当我尝试使用toSource()显示该对象时,我得到了类似的结果。

({state:(function (){return state;}), always:(function (){deferred.done(arguments).fail(arguments);return this;}), then:(function (){var fns=arguments;return jQuery.Deferred(function(newDefer){jQuery.each(tuples,function(i,tuple){var action=tuple[0],fn=fns[i];deferred[tuple[1]](jQuery.isFunction(fn)?function(){var returned=fn.apply(this,arguments);if(returned&&jQuery.isFunction(returned.promise)){returned.promise().done(newDefer.resolve).fail(newDefer.reject).progress(newDefer.notify);}else{newDefer[action+"With"](this===deferred?newDefer:this,[returned]);}}:newDefer[action]);});fns=null;}).promise();}), promise:(function (obj){return obj!=null?jQuery.extend(obj,promise):promise;}), pipe:(function (){var fns=arguments;return jQuery.Deferred(function(newDefer){jQuery.each(tuples,function(i,tuple){var action=tuple[0],fn=fns[i];deferred[tuple[1]](jQuery.isFunction(fn)?function(){var returned=fn.apply(this,arguments);if(returned&&jQuery.isFunction(returned.promise)){returned.promise().done(newDefer.resolve).fail(newDefer.reject).progress(newDefer.notify);}else{newDefer[action+"With"](this===deferred?newDefer:this,[returned]);}}:newDefer[action]);});fns=null;}).promise();}), done:(function (){if(list){var start=list.length;(function add(args){jQuery.each(args,function(_,arg){var type=jQuery.type(arg);if(type==="function"){if(!options.unique||!self.has(arg)){list.push(arg);}}else if(arg&&arg.length&&type!=="string"){add(arg);}});})(arguments);if(firing){firingLength=list.length;}else if(memory){firingStart=start;fire(memory);}}
return this;}), fail:(function (){if(list){var start=list.length;(function add(args){jQuery.each(args,function(_,arg){var type=jQuery.type(arg);if(type==="function"){if(!options.unique||!self.has(arg)){list.push(arg);}}else if(arg&&arg.length&&type!=="string"){add(arg);}});})(arguments);if(firing){firingLength=list.length;}else if(memory){firingStart=start;fire(memory);}}
return this;}), progress:(function (){if(list){var start=list.length;(function add(args){jQuery.each(args,function(_,arg){var type=jQuery.type(arg);if(type==="function"){if(!options.unique||!self.has(arg)){list.push(arg);}}else if(arg&&arg.length&&type!=="string"){add(arg);}});})(arguments);if(firing){firingLength=list.length;}else if(memory){firingStart=start;fire(memory);}}
return this;})})

有人能解释一下吗?我知道我的函数是异步的。

如何解决这个问题?

这是我的代码:

  module.Order = Backbone.Model.extend({
        initialize: function (attributes) {
            Backbone.Model.prototype.initialize.apply(this, arguments);
            this.pos = attributes.pos;
            this.sequence_number = this.pos.pos_session.sequence_number++;
            debugger;
            var odoo = []
            var call = this
            this.uid = this.generateUniqueId();
            this.pro = this.get_the_other_main().done(
                function (result) {
                }).always(function (result) {
                    odoo.push(result)
                    call.set({
                creationDate: new Date(),
                orderLines: new module.OrderlineCollection(),
                paymentLines: new module.PaymentlineCollection(),
                name: _t("Order ") + this.uid,
                client: null,
                sales_person: null,
                sales_person_name: null,
                new_id: odoo[0]
            })});
            alert(odoo[0])//// Must be adddddedd
            this.selected_orderline = undefined;
            this.selected_paymentline = undefined;
            this.screen_data = {};  // see ScreenSelector
            this.receipt_type = 'receipt';  // 'receipt' || 'invoice'
            this.temporary = attributes.temporary || false;
            return this;
        },
get_the_other_main: function () {
            var dfd = new jQuery.Deferred();
            new instance.web.Model("pos.order").call('get_the_product', []).done(
                function (results) {
                    var result = results.toString().split(',');
                    var stringsl = result[1];
                    var thenum = stringsl.replace(/^'D+/g, '');
                    var sasa = parseInt(thenum, 10) + 1
                    var zika = ('00' + sasa).slice(-4)
                    var the_str = result[1].slice(0, -4).toString();
                    var new_seq_sasa = the_str + zika
                    dfd.resolve(new_seq_sasa);
                }).always(function(results) {
                    var result = results.toString().split(',');
                    var stringsl = result[1];
                    var thenum = stringsl.replace(/^'D+/g, '');
                    var sasa = parseInt(thenum, 10) + 1
                    var zika = ('00' + sasa).slice(-4)
                    var the_str = result[1].slice(0, -4).toString();
                    var new_seq_sasa = the_str + zika
                    dfd.resolve(new_seq_sasa);
                }).always(function(results) {
                    var result = results.toString().split(',');
                    var stringsl = result[1];
                    var thenum = stringsl.replace(/^'D+/g, '');
                    var sasa = parseInt(thenum, 10) + 1
                    var zika = ('00' + sasa).slice(-4)
                    var the_str = result[1].slice(0, -4).toString();
                    var new_seq_sasa = the_str + zika
                    dfd.resolve(new_seq_sasa);
                });
            alert('')////If i remove that it will return undefind for this.pos
            return dfd

您似乎有异步调用的问题
(参见下面的评论)

// you call get_the_other_main which return a Promise ! 
        this.get_the_other_main().then(
            function (result) {
// when the Promise resolve you set this.pro, 
// what is this here ?? are you sure of the beahviour ?
//               |
//               V
                this.pro=result//got it right <---------------------- +
//                                                                    |
//                                                                    |
        });//                                                         |
// You set this.pro to another Promise, at this moment the previous this.pro is not set !
        this.pro=this.get_the_other_main().then(
            function (result) {
                 this.pro=result //got it right <----------------------------------------+
        }); //                                                                           |
// when you call alert, this.pro is a Promise not resolved !at this moment the previous this.pro is not set !
        alert(this.pro.toSource()) //[object object] 
// logicaly it show the code source of your deffered / Promise !

要解决您的问题,请这样尝试:

module.Order = Backbone.Model.extend({
  initialize: function(attributes) {
    var curOrder = this;
    Backbone.Model.prototype.initialize.apply(this, arguments);
    this.pos = attributes.pos;
    this.sequence_number = this.pos.pos_session.sequence_number++;
    debugger; // ??????
    this.uid = this.generateUniqueId();
    var odoo = []
    this.get_the_other_main().then(
      function(result) {
        curOrder.pro = result; //got it right
        curOrder.set({
          creationDate     : new Date(),
          orderLines       : new module.OrderlineCollection(),
          paymentLines     : new module.PaymentlineCollection(),
          name             : _t("Order ") + curOrder.uid,
          client           : null,
          sales_person     : null,
          sales_person_name: null,
          new_id           : curOrder.pro 
        });
        curOrder.selected_orderline   = undefined;
        curOrder.selected_paymentline = undefined;
        curOrder.screen_data          = {}; // see ScreenSelector
        curOrder.receipt_type         = 'receipt'; // 'receipt' || 'invoice'
        curOrder.temporary            = attributes.temporary || false;
       curOrder.trigger('orderready' , curOrder);
      });
    return this; 
// be careful because the process above is not done again, when you return this, it will be resolved later
  },
  get_the_other_main: function() {
    var dfd = new jQuery.Deferred();
    new instance.web.Model("pos.order").call('get_the_product', []).done(
      function(results) {
        var result = results.toString().split(',');
        var stringsl = result[1];
        var thenum = stringsl.replace(/^'D+/g, '');
        var sasa = parseInt(thenum, 10) + 1
        var zika = ('00' + sasa).slice(-4)
        var the_str = result[1].slice(0, -4).toString();
        var new_seq_sasa = the_str + zika
        dfd.resolve(new_seq_sasa);
      });
    return dfd
  },