Code Monkey home page Code Monkey logo

Comments (6)

typicode avatar typicode commented on May 22, 2024 1

Right now, the only workaround I can think of would be to use "real" strings for ids:

{
  "posts": [
    { "id": "e31aa48c", "body": "foo" } // or "id": "_1"
  ]
}

Because, when the server receive a request, it tries to turn parameters (which are always strings) to their native counterparts.

For example, parameters for http://api.example.com/posts?number=1&&boolean=true will be automatically translated from '1' and 'true' to 1 and true.

This is a good thing because in your database file you can use real integers and booleans.

{
  "posts": [
    { "id": 1, "body": "foo", "published": true, "views": 9001 }
  ]
}

For generating string ids, you can use https://github.com/broofa/node-uuid or https://github.com/typicode/underscore.db for generating a database file.

Does it solve your problem or not really?

from json-server.

typicode avatar typicode commented on May 22, 2024

I think I understand. Basically, you don't want to use integers for ids as shown in the example below?

{
  "posts": [
    { "id": 1, "body": "foo" }
  ]
}

from json-server.

davidemoro avatar davidemoro commented on May 22, 2024

Yep!

from json-server.

davidemoro avatar davidemoro commented on May 22, 2024

Hi @typicode, I've tried with the "e31a..." tip and it worked!

I just need to generate a static db file, so no need to use node-uuid or underscore.db in my case.

Thank you very much!

from json-server.

typicode avatar typicode commented on May 22, 2024

Great :) you're welcome.

from json-server.

pixelomo avatar pixelomo commented on May 22, 2024

I seem to be having the reverse problem, when I POST comments to the db the api generates postId as a string. The id generated is an integer. The comment doesn't show on the post since the postId is a string, if I manually update the db changing the postId to an integer the comment shows up.

Thought it might be an issue with parsing the JSON in my initial GET, tried parseInt(post.id) on my posts so that the id is an integer within the app but still when POSTing the postId generated is a string..

{
    "user": "Albert",
    "content": "I agree entirely",
    "date": "2016-03-18",
    "postId": "3",
    "id": 9
}

the data I'm POSTing is

{"user": "Albert", "content": "I agree entirely", "date": "2016-03-18"}

from json-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.