我得到这个错误,我不知道如何解决它.在节点js服务器中,我在promise上使用了一些.then()函数,最后我放置了一个由于某种原因无法识别的.catch()函数.我在教程中的许多地方都看到过这是错误的处理方式.我没有使用任何外部库.
错误 :
TypeError: promise.then(...).then(...).then(...).then(...).catch is not a function
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
exports.joinAlbum = function(req, res){
var promise = Album.findOne().where({'shortId': req.body.shortId}).exec(); // Returns a promise
promise.then(function(albumDoc){
console.log('Then #1');
.....
}
return albumDoc.save(); // Returns a promise
})
.then(function(albumDoc){
console.log('Then #2');
.....
return User.findById(req.body.userId).exec(); // Returns a promise
})
.then(function(userDoc){
console.log('Then #3');
........
return userDoc.save(); // Returns a promise
})
// Return a response
.then(function(savedUserDoc){
console.log('Then #4');
return res.status(200).json({success: true});
})
//Error handler
.catch(function(err){
console.log('Catch #1');
console.log(err);
res.status(200).json({success: false});
});
}
Run Code Online (Sandbox Code Playgroud)
如果.catch()不是处理承诺错误的正确方法,你有什么建议?我试图避免使用外部库,而更喜欢使用原生的JavaScript
编辑:解决方案 …
我试图使用Angular从远程WS获取Json格式的数据,我遇到了一些麻烦.数据来自Web服务正确,但我不能在控制器内使用它.这是为什么?角度代码:
var booksJson;
var app = angular.module('booksInventoryApp',[]);
// get data from the WS
app.run(function ($http) {
$http.get("https://SOME_API_PATH").success(function (data) {
booksJson = data;
console.log(data); //Working
});
});
app.controller('booksCtrl', function ($scope) {
$scope.data = booksJson;
console.log($scope.data); //NOT WORKING
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<section ng-controller="booksCtrl">
<h2 ng-repeat="book in data">{{book.name}}</h2>
</section>
Run Code Online (Sandbox Code Playgroud) 当我在react-native中按下输入时,键盘弹出一个打开.我想在按下某个按钮时关闭键盘(仅限).我怎样才能做到这一点?
在我的 React with Webpack 项目中,我将一些模块声明为全局变量,这样我就不必每次想使用它们时都导入它们。
在我的 Webpack 文件中:
plugins: [
new webpack.ProvidePlugin({
'_': 'lodash',
'React': 'react',
'moment': 'moment'
})
]
Run Code Online (Sandbox Code Playgroud)
现在我可以在任何地方使用 _,React,moment 而无需显式导入它。
将 Typescript 添加到项目后,我收到如下错误:
'moment' refers to a UMD global, but the current file is a module. Consider adding an import instead.
'_' refers to a UMD global, but the current file is a module. Consider adding an import instead.
Run Code Online (Sandbox Code Playgroud)
我的 tsconfig 文件:
{
"compilerOptions": {
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"module": "commonjs",
"noImplicitAny": true,
"outDir": "./build/",
"preserveConstEnums": true, …Run Code Online (Sandbox Code Playgroud) 我知道这个问题被问了很多次,但我无法让它按照我的需要正常工作。在 node.js ws 中,我将对象推送到某个数组中,如下所示:
var arr = [];
arr.push({"name": "someName", "id": 12345});
Run Code Online (Sandbox Code Playgroud)
最后我将其作为响应中的 json 发送。我怎样才能推入数组,以便能够提取这样的数据(在客户端):** ID 号是唯一的
var name = dataArr[12345]; //will return 'someName'
Run Code Online (Sandbox Code Playgroud)
我试图避免迭代整个数组来获取特定值的需要。
javascript ×2
angular-http ×1
angularjs ×1
arrays ×1
node.js ×1
promise ×1
react-native ×1
reactjs ×1
typescript ×1
webpack ×1