How to save a json object to mongodb, which is the answer to an API post

Asked

Viewed 601 times

0

I need to save a certain field,from a json object,which comes from the response of an API I called.

Save some fields of this response json object,in some fields of my colletion/model on Mongodb also using the package: Mongoose,Node/Express.

I print from the date/report which is the complete return object, but I only want the field number_token that is the field I need to save in the database.

So in the answer I select only the number_token to be returned:

Return res.json(data.data.number_token);

Example:

.then(data => {
      //console.log("data", data);
      return res.json(data.data.number_token);
}


It returns me the correct value of number_token.With each call I’ll have a new one number_token.Now I need to save each response,the response number_token,in my colletion/model’s number_token in Mongodb.


"9a17a5712c46a28ca824183e46b2e8e68f40c4b3ff136ed9251eda43904bfb00f7891e7c979473172ff0756860a7796bf8ce386e46c80a0b3b69410c9739c4c3"

Now I need to save this variable in the bank for each new request. number_token bank mongodb,lib Mongoose,express,.

Follows the model:

  number_token: {         
    type: Number,
    //required: true
  },

I can pass any user input with the req.body and save the fields passed in my bank with the function of Mongoose .save();.

But I still don’t understand the logic needed to save this return from number_token in my bank. I already have the connected model and the field number_token model. The problem lies in the logic of how to take this return and save in the database.

I thought I’d do it like this:


.then(function(response) {
      //console.log(JSON.stringify(response.data));
      //console.log(JSON.stringify(response.data.token));
      //res.json(response.data.token);

      const {number_token } =  res.body.token;
      
       const numberToken = new MyModel({
         number_token,
       });

       numberToken.save();


    })
    .catch(function(error) {
      console.log(error);
    });


But I still don’t understand how to take that amount and save it in the bank. I thought the correct way would be to save the number_token in a variable, and then save it in the database,along with the data passed in req.body,.

  • Solved! It was necessary to save the received value of the answer,in a new variable and then save everything in the bank.It was a very simple process and I ended up taking a while to get the code right. 

.then(function(response) {
 //console.log(JSON.stringify(response.data));
 //console.log(JSON.stringify(response.data.token));
 

 const token_card = response.data.token;
 console.log(token_card);
 const cardToken = new MyModel({
 
 token_card,
 
 });

 cardToken.save();




  • You can post the answer to your question. There is no restriction on this.

  • @Danizavtz,I can’t publish answers,. :/

1 answer

0


Solved!

It was necessary to save the received value of the answer,in a new variable and then save everything in the bank.It was a very simple process and I ended up taking a while to hit the code.

 .then(function(response) {  

 const token_card = response.data.token;
 const cardToken = new MyModel({

 token_card, 

}); 

CardToken.save(); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.