我正在尝试从传单层发布数据,然后通过express运行它并使用第三方模块返回一个数组。
如果我像这样规定了电话中所需的ID
forecast = msw.forecast(1)
它工作正常。当我尝试使用来自帖子请求的spot_id时,它给了我:
TypeError: Cannot use 'in' operator to search for 'units' in 1
这是我在node.js服务器上使用的内容。任何帮助都会非常感激。
var express = require('express');
var app = express();
var msw = require('msw-api');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
msw.set({ apiKey: 'my-api-key' , units: 'UK' });
app.post('/', function(req, res){
spot_id = req.body.spot_id
forecast = msw.forecast(spot_id).then(function (forecast) {
res.send(forecast)
});
});
app.listen(3000, function () {
console.log('Listening on port 3000!');
});
答案 0 :(得分:1)
正如评论中所讨论的, req.body.spot_id 的值默认为字符串,integer
需要spot_id
作为参数,因此出错。
您所要做的就是将integer
解析为var spot_id = parseInt(req.body.spot_id)
,如下所示:
forecast
在将其作为参数提供给{{1}}之前。