我无法使用请求承诺发布

时间:2018-10-30 23:05:36

标签: javascript node.js request http-post

我的问题是我收到一个错误消息,告诉我我无法发出发布请求。我使用insertDocuments函数将消息插入数据库。我正在尝试制作'mongolab-data-api' node.js模块的面向承诺的版本。如果我的问题令人困惑,我表示歉意

//This is a function in a class that handles all mlab related things
var rp = require('request-promise')
var insertDocuments = options => {
    if (!options.database || !options.collectionName || !options.documents) throw new Error('Database name, Collection Name, and Document(s) are required')
    var opt = {
        uri: `https://api.mongolab.com/api/1/databases/${options.database}/collections`,
        qs: {
            apiKey: this.apiKey

        },
        method: 'POST',
        body: {
            documents: options.documents
        },
        headers: {
            'User-Agent': 'Request-Promise'
        },
        json: true // Automatically parses the JSON string in the response
    };
    return rp(opt)
}

var options = {
    database: 'lexybase',
    collectionName: 'evy-history',
    documents: msg
}
insertDocuments(options)

我收到此错误: 未处理的拒绝StatusCodeError:405-{“ message”:“不允许发布。”}

at new StatusCodeError (/rbd/pnpm-volume/b50cbf1c-9de1-48a8-8200-48301efdd80c/node_modules/.registry.npmjs.org/request-promise-core/1.1.1/node_modules/request-promise-core/lib/errors.js:32:15)

at Request.plumbing.callback (/rbd/pnpm-volume/b50cbf1c-9de1-48a8-8200-48301efdd80c/node_modules/.registry.npmjs.org/request-promise-core/1.1.1/node_modules/request-promise-core/lib/plumbing.js:104:33)

at Request.RP$callback [as _callback] (/rbd/pnpm-volume/b50cbf1c-9de1-48a8-8200-48301efdd80c/node_modules/.registry.npmjs.org/request-promise-core/1.1.1/node_modules/request-promise-core/lib/plumbing.js:46:31)

at Request.self.callback (/rbd/pnpm-volume/b50cbf1c-9de1-48a8-8200-48301efdd80c/node_modules/.registry.npmjs.org/request/2.88.0/node_modules/request/request.js:185:22)

at emitTwo (events.js:126:13)

at Request.emit (events.js:214:7)

at Request.<anonymous> (/rbd/pnpm-volume/b50cbf1c-9de1-48a8-8200-48301efdd80c/node_modules/.registry.npmjs.org/request/2.88.0/node_modules/request/request.js:1161:10)

at emitOne (events.js:116:13)

at Request.emit (events.js:211:7)

at IncomingMessage.<anonymous> (/rbd/pnpm-volume/b50cbf1c-9de1-48a8-8200-48301efdd80c/node_modules/.registry.npmjs.org/request/2.88.0/node_modules/request/request.js:1083:12)

at Object.onceWrapper (events.js:313:30)

at emitNone (events.js:111:20)

at IncomingMessage.emit (events.js:208:7)

at endReadableNT (_stream_readable.js:1064:12)

at _combinedTickCallback (internal/process/next_tick.js:139:11)

at process._tickCallback (internal/process/next_tick.js:181:9)

编辑:事实证明,问题在于需要指定collectionName 3d,并且应该已经

body:  options.documents

代替

body: {
    documents:options.documents
}

1 个答案:

答案 0 :(得分:2)

您在 uri 中缺少集合名称:

uri:https://api.mongolab.com/api/1/databases/${options.database}/collections

应为:

uri:https://api.mongolab.com/api/1/databases/${options.database}/collections/{collectionName}

只需更改 collectionName

405从mongolab API返回