0

Hi guys i have a problem merging two objects any idea how can i do this ?

controller.hears('offres','message_received, facebook_postback',function(bot,message){

connection.connect("mongodb://localhost/bot",function(err,db){
    var z ="";
    var data = db.collection('offres').find().toArray().then(function(data){
        for (var i =0 ; i<10; i++){
            var _under = require("underscore");
            var titre = data[i]['titre'];
            var ref = data[i]['ref'].toString();
            var description = data[i]['description'];

            b = setpayload(titre,description, ref,'https://cdn.pixabay.com/photo/2017/01/13/17/29/hiring-1977803_960_720.jpg');
              z= _under.extend(z, b);
          }
        bot.reply(message,{
            attachment:{
            'type': 'template',
            'payload': {
                'template_type': 'generic',
                'elements':[z    ]
            }}
        }) 



    })
});


function setpayload(titre,sub, ref, image)
{
var m = {
            'title': titre,
            'image_url': image,
            'subtitle': sub,
            'buttons': [

                {
                    'type': 'postback',
                    'title': 'postuler',
                    'payload': ref
                },
                {
                    'type': 'postback',
                    'title': 'plus de détail',
                                'payload': ref
                }
            ]
        }

return m ;

}

notant que pour un slide ça marche tré bien controller.hears('offres','message_received, facebook_postback',function(bot,message){

connection.connect("mongodb://localhost/bot",function(err,db){

    var data = db.collection('offres').find().toArray().then(function(data){

        var _under = require("underscore");
        var titre = data[0]['titre'];
        var ref = data[0]['ref'].toString();
        var description = data[0]['description'];

        b = setpayload(titre,description, ref,'https://cdn.pixabay.com/photo/2017/01/13/17/29/hiring-1977803_960_720.jpg');


        bot.reply(message,{
            attachment:{
            'type': 'template',
            'payload': {
                'template_type': 'generic',
                'elements':[b]
            }}
        }) 



    })
});


function setpayload(titre,sub, ref, image)
{
var m = {
            'title': titre,
            'image_url': image,
            'subtitle': sub,
            'buttons': [

                {
                    'type': 'postback',
                    'title': 'postuler',
                    'payload': ref
                },
                {
                    'type': 'postback',
                    'title': 'plus de détail',
                                'payload': ref
                }
            ]
        }

return m ;

}

shaghabo
  • 1
  • 1

2 Answers2

0

there is a little mistak the require ('underscore') should no be there ..it is irrelevant to the problem anyways

shaghabo
  • 1
  • 1
0

Your setting z equal to a string before you try and merge them. It needs to be an object.

var z = {};

Also, why don't you just use this to merge your objects:

z = Object.assign(z, b);

Example:

$(function(){
  var z = {"foo": "bar"};
  
  var b = {"newData": "blah"};
  
  z = Object.assign(z, b);
  
  console.log(z);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here's the docs for Object.assign

matt
  • 1,680
  • 1
  • 13
  • 16