node.js 错误:options.uri 是必需的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40063875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:45:35  来源:igfitidea点击:

Error: options.uri is a required argument

node.jsexpressrequestbody-parser

提问by LoneWolf

I'm using node.js v4.6.0 and the latest versions of express, request and body-parser, yet i'm getting an error code which I can't fix, any ideas?

我正在使用 node.js v4.6.0 和最新版本的 express、request 和 body-parser,但我收到了一个无法修复的错误代码,有什么想法吗?

Here's my code:

这是我的代码:

var express = require('express');
var request = require('request');
var bodyparser = require('body-parser');

var app = express();
app.use(bodyparser.urlencoded({extended: true}))

    var webhook = process.env.DISCORD_WEBHOOK;

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

app.post('/webhook', (req, res) =>{
    request({
        method: 'POST',
        url: webhook,
        json: {
            "content": req.body.msg,
            "username": "Potato"
        }
    });

    res.redirect("/");
});

app.listen(80, () => {
    console.log("Server Started!");
});

and the error message i'm receiving:

以及我收到的错误消息:

Error: options.uri is a required argument
   at Request.init (C:\Users\kingn\node_modules\request\request.js:233:31)
   at new Request (C:\Users\kingn\node_modules\request\request.js:129:8)
   at request (C:\Users\kingn\node_modules\request\index.js:55:10)
   at C:\Users\kingn\index.js:15:5
   at Layer.handle [as handle_request]     (C:\Users\kingn\node_modules\express\lib\router\layer.js:95:5)
   at next (C:\Users\kingn\node_modules\express\lib\router\route.js:131:13)
   at Route.dispatch     (C:\Users\kingn\node_modules\express\lib\router\route.js:112:3)
   at Layer.handle [as handle_request] (C:\Users\kingn\node_modules\express\lib\router\layer.js:95:5)
   at C:\Users\kingn\node_modules\express\lib\router\index.js:277:22
   at Function.process_params (C:\Users\kingn\node_modules\express\lib\router\index.js:330:12)

Any fixes?

任何修复?

回答by Anand K Sant

You need to provide URL information while making a request, check to see that correct URL is getting assigned to your webhook variable, i.e

您需要在发出请求时提供 URL 信息,检查是否已将正确的 URL 分配给您的 webhook 变量,即

var webhook = process.env.DISCORD_WEBHOOK; //webhook should be assigned a valid URL for example: 'https://stackoverflow.com/'

var webhook = process.env.DISCORD_WEBHOOK; //webhook 应该被分配一个有效的 URL,例如:' https://stackoverflow.com/'

try console.log(webhook);

试试 console.log(webhook);

to find out URL for which you are making a request.

找出您要请求的 URL。

回答by Jacopo Brovida

Object request has parameter uri not url. https://github.com/request/request#multipartrelated

对象请求具有参数 uri 而不是 url。 https://github.com/request/request#multipartrelated

 request({
    method: 'POST',
    uri: webhook,
    json: {
        "content": req.body.msg,
        "username": "Potato"
    }
});