Code Monkey home page Code Monkey logo

Comments (15)

zackyang000 avatar zackyang000 commented on July 19, 2024 1

Hi, @fungwan

因为使用 node-odata 默认的 id 类型为 uuid, 如果你要使用 ObjectId 作为 id 类型的话, 需要进行显示声明.

示例如下:

var bookInfo = {
  _id: require('mongoose').Schema.ObjectId,  // <------ 显示声明为 ObjectId
  author: String,
  description: String,
  genre: String,
  price: Number,
  publish_date: Date,
  title: String
};

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

可以的吧, url 中是没有引号的.

http://books.zackyang.com/book(6c737509-a431-4b06-ba01-e7cb253b6fad)

from node-odata.

fungwan avatar fungwan commented on July 19, 2024

对的,我是没有用引号。不过现在可以了。之前的id号是以mongodb默认的objectID值查询的,ObjectId("5655697ac5cf723f0b7939c9"),/books(5655697ac5cf723f0b7939c9),就不行,但是换成作者你的uuid的这种格式就可以了。请问这个跟格式有关吗?我用你的API去新建一条记录,然后再用新生成的这条记录的id去查询就可以。生成的这个id格式就跟自己去mongo新增一条记录的格式就不一样

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

用 ObjectId 理论上来说应该是没问题的. 只要能通过下面这行代码查询出来就可以了.

https://github.com/TossShinHwa/node-odata/blob/master/src/rest/get.js#L5

有具体的 demo 吗?

from node-odata.

fungwan avatar fungwan commented on July 19, 2024

作者你好,我之前断点调试到你链接给出的地方,返回还是404。我就是用的你官网的例子运行的,数据文件也都是你test/support里面的books.json,但是GEt (ObjectId)类型就不行。楼主有时间的话可以试试看看呢。

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

Hi @fungwan, 是这个例子吗? 但是我例子中默认插入的数据应该是 uuid 的格式呢. 意思是你用的原来的数据? 这个我需要测试一下才知道原因.

from node-odata.

fungwan avatar fungwan commented on July 19, 2024

你好,我没有按照你的例子进行插入数据,而是我自己手动将数据通过mogouve可视化工具导入到mongodb的,所以生成的格式不是uuid,而是 ObjectId,所以再用你的GET/books(:id)这个API查询就不成功。

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

好的, 稍后我试试.

from node-odata.

fungwan avatar fungwan commented on July 19, 2024

作者你好,怎么样,确认重现了没?

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

是的, 确实有这个问题. 大概是因为 mongoose 查询时, 如果是 ObjectId, 需要做一个转换才行, 不能直接用字符串来查 ObjectId.

建议直接用该服务提供的 API 来插入数据.

from node-odata.

fungwan avatar fungwan commented on July 19, 2024

感谢回复。作者你的意思是现有的该服务不能满足以ObjectId的方式进行查询哇?主要是我这边mongodb里面的数据是其他服务程序插入进去的,其id类型就是默认的ObjectId。另外你提到的转换是指怎样的操作呢?

from node-odata.

fungwan avatar fungwan commented on July 19, 2024

好的,谢谢作者。抽空试试。

from node-odata.

netssfy avatar netssfy commented on July 19, 2024

显示声明_id可以解决get的问题
但是$filter=belongTo%20eq%2056775b9abf29cde453327f98
其中belongTo这个field是一个ObjectId类型,HTTP返回500, 结果为
Syntax error at '56775b9abf29cde453327f98'.

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

@netssfy, 是的, 会有这个问题. 但是一般 id 都直接 /resource(:id) 直接查询吧. 很少会用 $filter 的情况.

稍后我会修复这个问题.

谢谢.

from node-odata.

zackyang000 avatar zackyang000 commented on July 19, 2024

Fixed on v0.7.13.

from node-odata.

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.