Ginxxx
Ginxxx

Reputation: 1648

JSON.Stringify() force int value javascript(node.js)

Good day everyone i've been trying to force the data of my Json.stringify to int because on my mongodb it returns as string so on my schema i need to do it like this first

var UserSchema = mongoose.Schema({
    username:{
        type: String,
        index:true
    },
    influenceid:{
        type:String
    },
    question:{
        type:String
    },
    amount:{
        type:String //i need this to be a type:Number instead of string
    }
});

and on my dialog.js where in i put the json data

 socket.emit('dollar quest', JSON.stringify(result[0]), 
 JSON.stringify(result[1]), inUserId, myuserid, 'dquest');

and on my server i fetch data like this to throw it to my mongodb

socket.on('dollar quest', function(dolquest,dolamnt,uid,stud,stat){
    var info = new InfoUser({
        username: stud,
        amount: dolamnt,
        question: dolquest
    });

    InfoUser.createUser(info,function(err,user){
        if(err)throw err;
        console.log(user);
    });
});

But the output on my mlab is like this

"username": "stsam1",

"amount": "\"12500\"",

"question": "\"how are you\"",

How can i turn my amount into type:Number so that it will return on my mlab as like this

"amount": 12500,

Upvotes: 0

Views: 3325

Answers (3)

Ginxxx
Ginxxx

Reputation: 1648

I dig this one and it works on me so i'll answer my own question but thank you for the help sir T.J and Shaib. Here's what i did.

 socket.emit('dollar quest', JSON.stringify(result[0]), 
 JSON.stringify(result[1]), inUserId, myuserid, 'dquest');

and added

 socket.emit('dollar quest', JSON.stringify(result[0]), 
 JSON.stringify(result[1]).replace(/\"/g, ""), inUserId, myuserid, 'dquest');

So the idea was replacing so Sir TJ was has the right idea

Upvotes: 0

Shaishab Roy
Shaishab Roy

Reputation: 16805

You can make amount field is Number then it will automatically convert to Number from string

amount:{
   type: Number
}

or you can use parse.float like

socket.on('dollar quest', function(dolquest,dolamnt,uid,stud,stat){
    var info = new InfoUser({
        username: stud,
        amount: parseFloat(dolamnt),
        question: dolquest
    });

    InfoUser.save(function(err,user){
        if(err)throw err;
        console.log(user);
    });
});

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074268

You can provide a replacer function to JSON.stringify which will convert the value to number when the key name is "amount":

function theReplacer(key, value) {
    return key === "amount" ? +value : value;
}

then

var json = JSON.stringify(yourObject, theReplacer);

function theReplacer(key, value) {
    return key === "amount" ? +value : value;
}
var object = {
    username: "a",
    influenceid: "b",
    question: "c",
    amount: "42"
};
var json = JSON.stringify(object, theReplacer, 2);
console.log(json);

That example will convert the value of any "amount" field to a number, but you can refine it if you need to limit that.

Upvotes: 2

Related Questions