0

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,

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

but my JSON.Stringify(result[1]) is my dolamnt on my server and on my schema when i try to change it to type:number is doesn't work and doesn't return an error why is that??
@TheGinxx009: "dolamnt" isn't a word in the English language, and I'm afraid I can't infer what you meant there. You asked how to ensure that the amount, which is a string in your object, is a number in the JSON. The above is how you do that.
okay i get it now . dolamnt was being shorten cause it is a variable "dolaramount" is what its meant.
correction sir . i'm sorry but the string wasn't an object . it's a general variable
0

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);
    });
});

7 Comments

which one you tried ? N.B: should Number instead number
type:Number on my schema and parseFloat(dolamnt), on the server side
When i try this it always says infousers validation failed
you may trying to same username can try by different user name and try InfoUser.save like my code
sorry sir but i can't understand what you're saying
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.