2

Hi i am trying to push data on to JSON object which has key already defined but i am still getting uncaught typeError at line where i have alert("3") and alert("6")

 server.on('update_message_from_server', function(data){
                if(data.message_to!=false){
                    alert("1");
                    if(data.message_from==<?php echo $session_user_id;?>){
                        alert("2");
                        if(message_store.hasOwnProperty(data.message_to_email)){
                            alert("3");
                            message_store[data.message_to_email].push((data));
                            str2 = '<div id ="recent_message_log_entry'+data.message_to_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                            $(document.getElementById("recent_message_log_entry"+data.message_to_email)).replaceWith(str2);
                        }else{
                            alert("4");
                            message_store[data.message_to_email]= new Array();
                            message_store[data.message_to_email]=data;
                            string = '<div><div>'+data.message_to_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                            $("#new_updates").append(string);
                        }
                    }else{
                        alert("5");
                        if(message_store.hasOwnProperty(data.message_from_email)){
                            alert("6");
                            message_store[data.message_from_email].push((data));
                            str2 = '<div id ="recent_message_log_entry'+data.message_from_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                            $(document.getElementById("recent_message_log_entry"+data.message_from_email)).replaceWith(str2);
                        }else{
                            alert("7");
                            message_store[data.message_from_email]= new Array();
                            message_store[data.message_from_email]=data;
                            string = '<div><div>'+data.message_from_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                            $("#new_updates").append(string);
                        }
                    }
                }else{
                    alert("8");
                    alert("Email Does not exist "+data.message_to_email);
                }
                alert(JSON.stringify(message_store));
            });

How do i rectify this?

Thanks in advance


make request and response available in scope node js

I have this module.exports

module.exports = {
  defaultStrings: function() {
    return "Hello World" +
           "Foo - Bar";
  },
  urlSlug: function(s) {
    return s.toLowerCase().replace(/[^\w\s]+/g,'').replace(/\s+/g,'-');
  }
};

I want to be able to use request or response inside function defaultStrings how would I include that with a minimum change of the given code?

  defaultStrings: function(req, res, next) { // <--- This is not working it says Cannot call method 'someGlobalFunction' of undefined
    return "Hello World" +
           "Foo - Bar" + req.someGlobalFunction();
  },

In my app.js I require the file in

strings = require('./lib/strings'),

And this is how it is being called inside app.js

app.get('/',
        middleware.setSomeOperation,
        routes.index(strings);
4

1 回答 1

2

如果message_store[data.message_from_email]未定义,则首先为其分配一个空数组,然后将数据推送到数组以覆盖数组data

错误在行

message_store[data.message_from_email]= new Array();
message_store[data.message_from_email]=data;

它应该是

message_store[data.message_from_email] = new Array();
message_store[data.message_from_email].push(data);

或缩短

message_store[data.message_from_email]= [data];

所以

server.on('update_message_from_server', function(data){
    if(data.message_to!=false){
        alert("1");
        if(data.message_from==<?php echo $session_user_id;?>){
            alert("2");
            if(message_store.hasOwnProperty(data.message_to_email)){
                alert("3");
                message_store[data.message_to_email].push((data));
                str2 = '<div id ="recent_message_log_entry'+data.message_to_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                $(document.getElementById("recent_message_log_entry"+data.message_to_email)).replaceWith(str2);
            }else{
                alert("4");
                message_store[data.message_from_email]=[data];
                string = '<div><div>'+data.message_to_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                $("#new_updates").append(string);
            }
        }else{
            alert("5");
            if(message_store.hasOwnProperty(data.message_from_email)){
                alert("6");
                message_store[data.message_from_email].push((data));
                str2 = '<div id ="recent_message_log_entry'+data.message_from_email+'"><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                $(document.getElementById("recent_message_log_entry"+data.message_from_email)).replaceWith(str2);
            }else{
                alert("7");
                message_store[data.message_from_email]=[data];
                string = '<div><div>'+data.message_from_email+'</div><div>'+data.message+'</div><div>'+data.date_posted+'</div></div>';
                $("#new_updates").append(string);
            }
        }
    }else{
        alert("8");
        alert("Email Does not exist "+data.message_to_email);
    }
    alert(JSON.stringify(message_store));
});
于 2013-08-08T03:47:32.367 回答