【问题标题】:The [object object] can't be determined and shown in console[object object] 无法确定并显示在控制台中
【发布时间】:2015-11-04 17:16:22
【问题描述】:

我有一个函数可以返回类似 [object object] no my value that I want

当我尝试使用 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

【问题讨论】:

  • 这可能是您的代码在执行之前没有等待异步调用返回。所以你没有得到你期望的类型的对象。检查以确保您的回调设置正确。
  • 已经检查过 ,,, 请阅读我的代码中的 cmets .. 我该如何安排调用?
  • 即使您进行了编辑,您的this.set 也会出现在this.get_the_other_main().done 之前,所以这是错误的!
  • 必须 read this 才能解决您的问题

标签: javascript jquery asynchronous openerp point-of-sale


【解决方案1】:

您似乎遇到了异步调用的问题。
(请参阅下面的 cmets)

// 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
  },

【讨论】:

  • 我通过一个技巧解决了我的问题,但我在这里遇到了一个问题,我更新了代码查看我的 get_the_other_main 函数,当我添加该警报时,它会正确打印值,当我删除了那个警报..它返回 undefind ,那么我怎么能完成我的把戏?
  • 代码更新了,代码你解释为什么会这样?我怎么能欺骗这个警报?
  • @MostafaMohamed 我已经更新了反映您需要的答案!
  • 你的代码不起作用,我可以在结果函数中运行 set 函数!
  • 请查看我的代码,它可以工作,但我必须使用最后一个空警报来正确打印值:
猜你喜欢
  • 2019-07-21
  • 2019-12-02
  • 2020-02-18
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 2016-10-05
  • 1970-01-01
相关资源
最近更新 更多