我目前正在使用Nodejs。当我从另一个文件调用函数时,它显示以下错误
error: uncaughtException: Cannot read property 'params' of undefined
这是我从另一个文件中调用的函数
exports.signupconfirm = function(req,res) {
var id = req.params.id;
var obj_id = BSON.ObjectID.createFromHexString(id);
logger.log('info','FILE ['+__filename.split('/').pop()+'] : LINE ['+stackTrace.get()[0].getLineNumber()+'] : Signup Confirm ID: '+id);
logger.log('info','FILE ['+__filename.split('/').pop()+'] : LINE ['+stackTrace.get()[0].getLineNumber()+'] : Signup Confirm ObjectID: '+obj_id);
dbconnection.Ucollection.find({"_id":obj_id}).toArray(function(err,result) {
if(err)logger.log('info','FILE ['+__filename.split('/').pop()+'] : LINE ['+stackTrace.get()[0].getLineNumber()+'] : Check the Database Connection details from SIGNUPCONFIRM');
var confirm = result[0].confirmation;
}
答案 0 :(得分:1)
/api/signup/:id
始终引用路径路径中的变量。
如果你没有传递任何id参数,它将是未定义的(就像你的情况一样)。
就像这里提到的@chenkehxx一样,你需要通过路径传递id。
假设/api/signup/1
而不是req.params
将获取1的id,然后#import "ViewController.h"
#import "TechStars.h"
#import "InfoViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.techstars = [TechStars stars];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.techstars count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
- (UIImage *)imageScaledToSize:(UIImage *)image size:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"employeeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell detailTextLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell detailTextLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell detailTextLabel] setFont:[UIFont systemFontOfSize: 12.0]];
NSDictionary *employee = self.techstars[indexPath.row];
cell.textLabel.text = [employee objectForKey:@"employeename"];
cell.detailTextLabel.text = [employee objectForKey:@"position"];
cell.imageView.image = [self imageScaledToSize :[employee objectForKey:@"profilePicture"] size:CGSizeMake(95, 75)];
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UITableViewCell class]])
{
if ([segue.destinationViewController isKindOfClass:[InfoViewController class]])
{
InfoViewController *nextViewController = segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForCell:sender];
TechStars *selectedObject = [self.techstars objectAtIndex:path.row];
// this also can be written as self.techstars[path.row]; literal syntax
nextViewController.stars = selectedObject;
}
}
}
InfoViewController.h
// Test
//
// Created by 123 on 10/07/16.
// Copyright © 2016 123. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TechStars.h"
#import "ViewController.h"
@interface InfoViewController : UIViewController
@property (strong, nonatomic) TechStars *stars;
@property (strong, nonatomic) IBOutlet UILabel *employeeName;
@property (strong, nonatomic) IBOutlet UIImageView *employeeImage;
@property (strong, nonatomic) IBOutlet UILabel *employeeDuties;
@end
// InfoViewController.m
// Test
//
// Created by 123 on 10/07/16.
// Copyright © 2016 123. All rights reserved.
//
#import "InfoViewController.h"
#import "ViewController.h"
@interface InfoViewController ()
@end
@implementation InfoViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
将返回1而不是未定义。
答案 1 :(得分:0)
我对您的项目不太确定,但是可以通过一种不同的方式通过对象指针传递它:
nested = new ArrayList()
应纠正该错误。