search

Home  >  Q&A  >  body text

How to get data from key-value pairs in Vue.js

<p>I use rails on the backend and vue.js on the frontend. I'm trying to print the error just in case. Under <code>.catch</code> I get an error like below but cannot get the message from it. Please help me solve this problem. </p> <pre class="brush:php;toolbar:false;">.catch(function (error) { debugger });</pre> <p>In the console, if I try <code>error.response.data.error</code> this returns <code>'{:message=>"Amount is less than the minimum value"} '</code> I can't figure out how I can get only the message. </p> <p>Wrong answer</p>
P粉418854048P粉418854048458 days ago456

reply all(1)I'll reply

  • P粉337385922

    P粉3373859222023-09-03 12:52:42

    Your baclend is not serializing the object correctly. So the message is part of a string and not a JSON property. If you don't want to change you can use

    const message = error.response.data.error.substring(
        str.indexOf('"') + 1, 
        str.lastIndexOf('"')
    );

    reply
    0
  • Cancelreply